android – Use two RecyclerView with different adapter inside NestedScrollView load very slowly-ThrowExceptions

Exception or error:

I need to use two RecyclerView with different Adapter in the same view.

My First RecyclerView will load just once in onCreate.

My Second RecyclerView will load more and more data when user scroll to bottom and call the api to get new data.

When I let my Second RecyclerView call the api to load more and more data, it will load very slowly and scroll heavy.

How can I fix that, thank you!

Here is my fragment:

public class NewFragment extends Fragment {

    private void setRecyclerAndAdapter(){
        if(swipeRefreshLayout != null && postRecycler != null){

            layoutManager = new LinearLayoutManager(context); 
            postRecycler.setLayoutManager(layoutManager);

            LayoutAnimationController animationController = AnimationUtils.loadLayoutAnimation(context, R.anim.layout_animation_up_to_down);
            postRecycler.setLayoutAnimation(animationController);

            if(postAdAdapter == null){
                postAdAdapter = new PostCardAdapter(context, postDataList);
                postRecycler.setAdapter(postAdAdapter);

                setListener();
            }else{
                postAdAdapter.notifyDataSetChanged();
            }
        }else
            return;
    }
.
    private void setListener(){
        if(swipeRefreshLayout != null && postRecycler != null){

            ......

            nestedScrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
                @Override
                public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
                    if(v.getChildAt(v.getChildCount() - 1) != null) {
                        if ((scrollY >= (v.getChildAt(v.getChildCount() - 1).getMeasuredHeight() - v.getMeasuredHeight())) &&
                                scrollY > oldScrollY) {

                            visibleItemCount = layoutManager.getChildCount();
                            totalItemCount = layoutManager.getItemCount();
                            pastVisiblesItems = layoutManager.findFirstVisibleItemPosition();

                            if(controlCanGetMorePostData == true){
                                controlCanGetMorePostData = false;

                                if(postAdAdapter != null){
                                    if ((visibleItemCount + pastVisiblesItems) >= totalItemCount) {
                                        postAdAdapter.setLoadState(postAdAdapter.LOADING);
                                        callApiManagerGetMoreData();
                                    }
                                }else
                                    return;
                            }
                            else{
                                hasMorePostData = false;
                                setMoreDataState();
                            }
                        }
                    }
                }
            });
        }else
            return;
    }

}

Here is my Layout:

<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/fragment_new_swipe_refresh"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <androidx.core.widget.NestedScrollView
            android:id="@+id/fragment_new_nestedScroll"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:fillViewport="true"
            android:scrollbars="none">

                <GridLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:columnCount="1"
                    android:rowCount="2">

                        <RelativeLayout
                            android:id="@+id/fragment_new_banner_content"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_row="0">

                                <androidx.recyclerview.widget.RecyclerView
                                    android:id="@+id/fragment_new_first_recycler"
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:scrollbars="vertical" />

                                <com.gmpsykr.each.Game.BannerIndicator
                                    android:id="@+id/fragment_new_banner_indicator"
                                    android:layout_width="match_parent"
                                    android:layout_height="32dp"
                                    android:layout_alignBottom="@+id/fragment_new_banner_recycler"
                                    app:selectColor="@color/colorAccent"
                                    app:unselectedColor="#ffffff"
                                    app:radius="3dp"
                                    app:space="10dp" />

                        </RelativeLayout>

                        <androidx.recyclerview.widget.RecyclerView
                            android:id="@+id/fragment_new_second_recycler"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layoutAnimation="@anim/layout_animation_up_to_down"
                            android:layout_row="1" />

                </GridLayout>

        </androidx.core.widget.NestedScrollView>


</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
How to solve:
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/fragment_new_swipe_refresh"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

        <androidx.coordinatorlayout.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

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

                                <RelativeLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="50dp"
                                    android:background="@color/colorAppPrimaryRed" />

                                <androidx.recyclerview.widget.RecyclerView
                                    android:id="@+id/fragment_new_recycler"
                                    android:layout_width="match_parent"
                                    android:layout_height="match_parent"
                                    android:nestedScrollingEnabled="false"
                                    android:layoutAnimation="@anim/layout_animation_up_to_down" />

                        </LinearLayout>


        </androidx.coordinatorlayout.widget.CoordinatorLayout>

</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>

###

I want you to read this article and the same problem at https://stackoverflow.com/a/37337641/11560055.

I think this will be an answer for your problem.

Leave a Reply

Your email address will not be published. Required fields are marked *