在CoordinatorLayout内部的RecyclerView,AppBarLayout滚动问题

Taz*_*One 11 android smooth-scrolling android-recyclerview android-coordinatorlayout

我在片段中有这个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>
Run Code Online (Sandbox Code Playgroud)

在项目集上的Java类ClickListener:

@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);
                }
            }
Run Code Online (Sandbox Code Playgroud)

当我点击ImageButton时,我的RecyclerView滚动到位置,一切看起来都很好.但是,如果我把手指放在AppBarLayout部分(ImageButton)上,可见(粘性)在顶部,并拖动到底部我滚动不好.我的appbar开始扩展,而我的Recycler在顶部有一些元素(滚动时隐藏它们).

在此输入图像描述

我认为这个问题是设定行为.因为如果我首先滚动回收器,AppBar不会开始扩展,而Recycler不会富有元素.

谢谢你的回答.

Ami*_*yay 5

糟糕的滚动曾经发生在我身上,它发生是因为我RecyclerView在另一个RecyclerView.

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

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

recyclerView.setNestedScrollingEnabled(false);
Run Code Online (Sandbox Code Playgroud)

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

android:nestedScrollingEnabled="false"
Run Code Online (Sandbox Code Playgroud)


小智 5

有了这个,你告诉它“合并” RecyclerView 的滚动与其父级的滚动

app:layout_behavior="@string/appbar_scrolling_view_behavior"/>
Run Code Online (Sandbox Code Playgroud)

如果我很好理解,您希望具有以下滚动行为:

  • 如果您通过触摸 RecyclerView 外部来滚动,它会折叠 AppBar
  • 如果您通过触摸内部进行滚动,它会忽略 RecyclerView 的滚动并折叠 AppBar,一旦 AppBar 折叠,它就会在 RecyclerView 内部滚动

您能否确认这是所需的行为?

在这种情况下,你可以看看这个答案,也许它会有所帮助