在NestedScrollView中使用两个具有不同适配器的RecyclerView加载速度非常慢

问题描述 投票:1回答:1

我需要在同一视图中使用两个带有不同适配器的RecyclerView。

我的第一个RecyclerView将仅在onCreate中加载一次。

当用户滚动到底部并调用api以获取新数据时,我的Second RecyclerView将加载越来越多的数据。

[当我让Second RecyclerView调用api来加载越来越多的数据时,它将加载非常缓慢并且滚动繁琐。

我该如何解决,谢谢!

这是我的片段:

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;
    }

}

这是我的布局:

<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>
android android-recyclerview android-nestedscrollview
1个回答
0
投票
<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>
© www.soinside.com 2019 - 2024. All rights reserved.