RecyclerView inside CoordinatorLayout,AppBarLayout Scrolling issue

11,753

Solution 1

with this, you tell it to "merge" the scrolling of the RecyclerView with the scrolling of its parent

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

if I well understood, you want to have the following scrolling behaviour:

  • if you are scrolling by touching outside the RecyclerView, it collapses the AppBar
  • if you are scrolling by touching inside it, it ignores the RecyclerView's scroll and collapses the AppBar, and once the AppBar is collapsed, it scrolls inside the RecyclerView

Could you confirm this is the desired behaviour please?

In such case, you may look at this answer, maybe it will help

Solution 2

The bad scrolling once happened to me, it was happening because I was using RecyclerView inside another RecyclerView.

So when I try to scroll contents in the main RecyclerView it gave me this issue.

To resolve that issue I added this to RecyclerView:

recyclerView.setNestedScrollingEnabled(false);

For doing this in XML you can use:

android:nestedScrollingEnabled="false"
Share:
11,753
TazmanOne
Author by

TazmanOne

Android/IOS developer. Bachelor of Software Engineering.

Updated on June 12, 2022

Comments

  • TazmanOne
    TazmanOne almost 2 years

    I have this xml code in fragment:

    <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>
    

    in Java Class on Item set 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);
                    }
                }
    

    When I click to ImageButton, my RecyclerView scrolls to position, everything looks fine. But if I put finger on AppBarLayout section (ImageButton) which visible(sticky) on top, and drag to bottom I have a bad scrolling. My appbar start expanded, while my Recycler have some elements on top (they are hidden when scrolled).

    enter image description here

    I think this problem is setting behavoir. Because if I scrolling recycler first, AppBar doesnt start expanding, while Recycler not rich top of elements.

    Thanks for your answers.

  • r3flss ExlUtr
    r3flss ExlUtr over 6 years
    What's worse, this is actually completely backwards. You should not have this, it will block the intended behaviour
  • Krystian Bersztolc
    Krystian Bersztolc over 5 years
    This is correct way to making toolbar scrollFlags working in case of nested RecyclerViews (to make toolbar scroll working, place android:nestedScrollingEnabled="false" in nested RecyclerViews)