0

I am fetching data from my API using Retrofit library. The data is videos which are displayed in recyclerview.I am trying to implement infinite pagination in recyclerview but facing lagging issues after 4 or 5th page is loaded (i.e. after approximately 80 to 100 videos). Here is my XML and Java code:

XML code:

<androidx.core.widget.NestedScrollView
   android:id="@+id/nestedScrollView"
   android:layout_width="wrap_content"
   android:layout_height="wrap_content"
   android:fillViewport="true">

   <LinearLayout
       android:layout_width="match_parent"
       android:layout_height="match_parent"
       android:layout_marginLeft="10dp"
       android:orientation="vertical">

        <androidx.constraintlayout.widget.ConstraintLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content">

             <androidx.recyclerview.widget.RecyclerView
                 android:id="@+id/recyclerViewCategories"
                 android:layout_width="match_parent"
                 android:layout_height="wrap_content"
                 app:layout_constraintBottom_toBottomOf="parent"
                 app:layout_constraintEnd_toEndOf="parent"
                 app:layout_constraintStart_toStartOf="parent"
                 app:layout_constraintTop_toTopOf="parent">

             </androidx.recyclerview.widget.RecyclerView>

        </androidx.constraintlayout.widget.ConstraintLayout>

            <androidx.constraintlayout.widget.ConstraintLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginRight="10dp">

                <androidx.recyclerview.widget.RecyclerView
                    android:id="@+id/recyclerViewVideos"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    app:layout_constraintBottom_toBottomOf="parent"
                    app:layout_constraintEnd_toEndOf="parent"
                    app:layout_constraintStart_toStartOf="parent"
                    app:layout_constraintTop_toTopOf="parent">

                </androidx.recyclerview.widget.RecyclerView>

            </androidx.constraintlayout.widget.ConstraintLayout>

            <ProgressBar
                android:id="@+id/progress"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center"
                android:visibility="gone" />

    </LinearLayout>

</androidx.core.widget.NestedScrollView>

Java code:

    recyclerViewVideos.setNestedScrollingEnabled(false);

    nScrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
        @Override
        public void onScrollChanged() {
            View view = (View) nScrollView.getChildAt(nScrollView.getChildCount() - 1);

            int totalItems = gridLayoutManager.getItemCount();

            Log.d(TAG, "onScrollChanged: Total Items" + totalItems);

            int diff = (view.getBottom() - (nScrollView.getHeight() + nScrollView.getScrollY()));

            if (diff == 0 && !apiCalling) {
                Log.d(TAG, "onScrollChanged: API fetching");
                apiCalling = false;
                progressBar.setVisibility(View.GONE);
                CallRetrofitForHomeScreenVideos();
                apiCalling = true;
                progressBar.setVisibility(View.VISIBLE);
            }
        }
    });

Kindly let me know if you also require to check CallRetrofitForHomeScreenVideos() method's code.

I referred below provided solutions and also implemented all of them. The 4th solution is active and effective than all others:

  1. Real slow pagination with NestedScrollView

  2. Can not handle user pagination with GridLayoutManager

  3. Android RecyclerView adding pagination

  4. Pagination not work for the RecyclerView within NestedScrollView

  5. RecyclerView lags inside NestedScrollView

Suggestions from all are truly welcome. Let me know if anyone require more information or code in order to understand the issue and require to deploy research for this.

0 Answers0