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

时间:2017-01-16 10:00:22

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

我在片段中有这个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>

在项目集上的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);
                }
            }

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

enter image description here

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

感谢您的回答。

5 个答案:

答案 0 :(得分:4)

有了这个,你告诉它&#34;合并&#34;通过滚动其父级

滚动RecyclerView
app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

如果我很好理解,你想要有以下滚动行为:

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

请问您确认这是理想的行为吗?

在这种情况下,您可以查看this answer,也许会有所帮助

答案 1 :(得分:2)

糟糕的滚动一旦发生在我身上,它发生了,因为我在另一个RecyclerView内使用RecyclerView

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

要解决该问题,我将其添加到RecyclerView

recyclerView.setNestedScrollingEnabled(false);

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

android:nestedScrollingEnabled="false"

答案 2 :(得分:0)

我认为您需要在NestedScrollView中包装内容并在NestedScrollView中设置app:layout_behavior="@string/appbar_scrolling_view_behavior"

答案 3 :(得分:0)

如果您在ViewPager中使用RecyclerView,则将此行添加到ViewPager:android:nestedScrollingEnabled="false"

它将解决您的问题。

答案 4 :(得分:-1)

这可能很棘手,为了实现这一点,你需要做一些事情。

您应该在app:layout_scrollFlags="scroll|enterAlwaysCollapsed"而不是CollapsingToolbarLayout使用scroll

标签或按钮在XML布局中的位置并不清楚,但如果它们应该保留在屏幕上,那么您需要pin它们,因此您可以使用app:layout_collapseMode="pin"作为该元素。也许这是在LinearLayoutImageView

如果LinearLayout包含其他内容,那么你应该添加一些滚动标记,如果它应该滚动屏幕,最好是app:layout_scrollFlags="scroll|enterAlwaysCollapsed"

最后,请确保您不禁用RecyclerView中的嵌套滚动。