【问题标题】:RecyclerView inside CoordinatorLayout,AppBarLayout Scrolling issueCoordinatorLayout 内的 RecyclerView,AppBarLayout 滚动问题
【发布时间】:2017-05-31 03:13:15
【问题描述】:

我在片段中有这个 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)上,然后拖动到底部,我的滚动就会很糟糕。 我的 appbar 开始扩展,而我的 Recycler 顶部有一些元素(滚动时它们被隐藏)。

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

感谢您的回答。

【问题讨论】:

  • 您是否尝试在协作工具栏上应用 layout_behavouir ??
  • BottomTab 库的名称是什么?看起来很不错。
  • @Thracian 这是一个简单的布局,有自己的逻辑,ty
  • 您好,您找到解决方案了吗?
  • @NehaK 找到这个答案帮助我。解决方法:将根元素更改为 NestedScrollView,向其中添加回收器,然后将 nestedscrollingEnabled = true 添加到 NestedScrollView (>21ApI) 或使用 NestedScrollChildHelper 支持 libr

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


【解决方案1】:

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

所以当我尝试滚动主 RecyclerView 中的内容时,它给了我这个问题。

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

recyclerView.setNestedScrollingEnabled(false);

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

android:nestedScrollingEnabled="false"

【讨论】:

  • 更糟糕的是,这实际上是完全倒退的。你不应该有这个,它会阻止预期的行为
  • 这是使工具栏scrollFlags 在嵌套 RecyclerViews 的情况下工作的正确方法(要使工具栏滚动工作,请将 android:nestedScrollingEnabled="false" 放在嵌套的 RecyclerViews 中)
【解决方案2】:

这样,您告诉它“合并” RecyclerView 的滚动与其父级的滚动

app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

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

您能否确认这是您想要的行为?

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

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

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

      它会解决你的问题。

      【讨论】:

        【解决方案5】:

        这可能很棘手,您需要具备一些条件才能使其发挥作用。

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

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

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

        最后,确保您没有在 RecyclerView 中禁用嵌套滚动。

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-09-18
          • 2015-09-27
          • 1970-01-01
          • 1970-01-01
          • 2015-12-15
          • 2020-11-14
          • 1970-01-01
          相关资源
          最近更新 更多