TazmanOne
TazmanOne

Reputation: 573

RecyclerView inside CoordinatorLayout,AppBarLayout Scrolling issue

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.

Upvotes: 15

Views: 17558

Answers (5)

Rohail
Rohail

Reputation: 31

If you are using RecyclerView inside ViewPager then add this line to ViewPager: android:nestedScrollingEnabled="false"

It will solve your problem.

Upvotes: 0

Paparazi NDH
Paparazi NDH

Reputation: 1

I think you need wrap content in NestedScrollView and set app:layout_behavior="@string/appbar_scrolling_view_behavior" in NestedScrollView

Upvotes: 0

AdricoM
AdricoM

Reputation: 579

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

Upvotes: 5

user781729
user781729

Reputation:

It can be tricky and there's a few things you need to have in order for this to work.

You should use app:layout_scrollFlags="scroll|enterAlwaysCollapsed" in your CollapsingToolbarLayout instead of just scroll.

It's not clear where the tabs or buttons are in your XML layout, but if they are supposed to stay on screen then you need to pin them, so you would use app:layout_collapseMode="pin" for that element. Perhaps this is in the LinearLayout or ImageView?

If the LinearLayout holds something else then you should add some scroll flags to that too, probably best would be app:layout_scrollFlags="scroll|enterAlwaysCollapsed" if it is supposed to scroll off screen.

Lastly, make sure you are not disabling nested scrolling in your RecyclerView.

Upvotes: 0

Amit Upadhyay
Amit Upadhyay

Reputation: 7391

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"

Upvotes: 8

Related Questions