android - RecyclerView inside CoordinatorLayout,AppBarLayout 滚动问题

标签 android android-recyclerview android-coordinatorlayout smooth-scrolling

我在 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 顶部有一些元素(它们在滚动时隐藏)。

enter image description here

我认为这个问题是设置行为。因为如果我先滚动回收器,AppBar 不会开始扩展,而回收器的顶部元素不丰富。

感谢您的回答。

最佳答案

糟糕的滚动曾经发生在我身上,这是因为我在另一个 RecyclerView 中使用 RecyclerView

因此,当我尝试在主 RecyclerView 中滚动内容时,它给了我这个问题。

为了解决这个问题,我将此添加到 RecyclerView:

recyclerView.setNestedScrollingEnabled(false);

要在 XML 中执行此操作,您可以使用:

android:nestedScrollingEnabled="false"

关于android - RecyclerView inside CoordinatorLayout,AppBarLayout 滚动问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41673723/

相关文章:

android - 自定义 CoordinatorLayout 行为

android - ListViewItem 背景颜色

android - Android NDK 的实际用途是什么?

java - 页面滚动时 View 寻呼机内的回收器 View 不刷新

tabs - 尽管实现了所有必需的参数,但 CoordinatorLayout 不会在滚动时隐藏工具栏

java - 如何删除工具栏上方的空间?

android - C/C++ printfs - 它出现在 Android native 代码中的什么位置?

php - 通过未加密的 JSON 发送人名的足够安全的方法

android - RecyclerView onClick 用于多个按钮并从 Activity 处理

java - 取景器内的 getActivity()