Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 533
TazmanOne Avatar asked Jan 16 '17 10:01

TazmanOne


People also ask

Is recycler view scrollable?

To be able to scroll through a vertical list of items that is longer than the screen, you need to add a vertical scrollbar. Inside RecyclerView , add an android:scrollbars attribute set to vertical .

How do I disable scrolling in collapsing toolbar layout Android?

The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet.

What is the use of CoordinatorLayout in Android?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

What is CoordinatorLayout and ConstraintLayout?

Use Coordinatorlayout as the top-level application decor. It will usually used to layout AppBarLayout , FloatingActionButton , and the main body of your screen, say NestedScrollView . Inside the NestedScrollView use ConstraintLayout to describe the rest of the layout as a flat hierarchy.


1 Answers

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"
like image 155
Amit Upadhyay Avatar answered Sep 28 '22 07:09

Amit Upadhyay