我在 fragment 中有这个 xml 代码:
<CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/coordinatorLayout" android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme"
app:elevation="0dp">
<android.support.design.widget.CollapsingToolbarLayout
android:layout_width="match_parent"
android:layout_height="300dp"
app:layout_scrollFlags="scroll"
android:id="@+id/collapsingToolbarLayout"
app:statusBarScrim="@color/bestColor">
<LinearLayout></LinearLayout> <!--this elements hide then appbar is collapsed-->
</android.support.design.widget.CollapsingToolbarLayout>
<LinearLayout>
<ImageButton>
android:id="@+id/profile_header_trophies"
</ImageButton><!-- this elements like a tab,visible if appbar collapsed-->
</LinearLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/profile_recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
</android.support.design.widget.CoordinatorLayout>
在项目集 ClickListener 的 Java 类中:
@OnClick(R.id.profile_header_trophies)
public void profile_header_trophies_clicked() {
if (myProfile != null) {
appBarLayout.setExpanded(false, false);
if (myProfile.getBests().size() == 0) {
profile_recyclerView.smoothScrollToPosition(3);
} else {
profile_recyclerView.smoothScrollToPosition(2 + 20);
}
}
当我点击 ImageButton 时,我的 RecyclerView 滚动到位置,一切看起来都很好。 但是,如果我将手指放在顶部可见(粘性)的 AppBarLayout 部分(ImageButton)上,然后拖动到底部,则滚动效果不佳。 我的应用栏开始扩展,而我的 Recycler 顶部有一些元素(它们在滚动时隐藏)。
我认为这个问题是设置行为。因为如果我先滚动回收器,AppBar 不会开始扩展,而回收器的顶部元素不丰富。
感谢您的回答。
最佳答案
糟糕的滚动曾经发生在我身上,这是因为我在另一个 RecyclerView
中使用 RecyclerView
。
因此,当我尝试在主 RecyclerView
中滚动内容时,它给了我这个问题。
为了解决这个问题,我将此添加到 RecyclerView
:
recyclerView.setNestedScrollingEnabled(false);
要在 XML 中执行此操作,您可以使用:
android:nestedScrollingEnabled="false"
关于android - RecyclerView inside CoordinatorLayout,AppBarLayout 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673723/