Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Paging library when used inside a nestedscrollview loads all data

I am using paging library to load data and populate my recyclerview which is placed inside a nestedscrollview. But it is like, pagination works automatically until all the data fetched from API. I know this is because of the nestedscrollview. But unfortunately my layout needs scrollview as i have a top section other than recyclerview in this fragment. This is my layout

<android.support.v4.widget.NestedScrollView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fillViewport="true"
   >
   <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

   //have a layout here which scrolls with recyclerview

      <Recyclerview />

   </ConstraintLayout>

</NestedScrollView>

Everything works fine when i do not use nestedscrollview. There is an open issue in googlesamples git repo regsrding this problem.

https://github.com/googlesamples/android-architecture-components/issues/215

Is anyone have idea how can we implement pagination when a recyclerview is inside a scrollview with pagination library from Android jetpack. I know we can implement traditional kind of pagination attaching listener to nestedscrollview, but i am looking to implement pagination with architecture component library. https://developer.android.com/topic/libraries/architecture/paging/

like image 234
Pramod Moolekandathil Avatar asked Feb 06 '19 06:02

Pramod Moolekandathil


2 Answers

Using the below code will solve the issue. Here is the view hierarchy:

<androidx.coordinatorlayout.widget.CoordinatorLayout
    android:id="@+id/coordinator_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <com.google.android.material.appbar.AppBarLayout
        android:id="@+id/appbar_layout"
        android:background="@color/white"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.material.appbar.CollapsingToolbarLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            app:titleEnabled="false">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <!-- Put here elements that you need above the recycler view -->

            </LinearLayout>

        </com.google.android.material.appbar.CollapsingToolbarLayout>

    </com.google.android.material.appbar.AppBarLayout>
    <!-- RecyclerView -->
    <androidx.recyclerview.widget.RecyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:nestedScrollingEnabled="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"
        android:scrollbars="vertical"
        app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />

</androidx.coordinatorlayout.widget.CoordinatorLayout>

Note: Be sure that you have given id to CoordinatorLayout and AppBarLayout so that it will retain scroll position on the back stack.

like image 156
Kyo Huu Avatar answered Oct 26 '22 16:10

Kyo Huu


The problem is recyclerview inside nested scrolview. Paging library has nothing to do with it. The behavior would be same if you try to load data on scroll listener of recyclerview.

like image 1
Pramod Moolekandathil Avatar answered Oct 26 '22 15:10

Pramod Moolekandathil