如何在滚动时调用 RecyclerView 项目

How To Call RecyclerView Item while scrolling

我在我的 activity 片段中使用 RecyclerView,下面的 ConstraintLayout 是调用片段的地方,当我打开应用程序时,它会调用用户未打开的 RecyclerView 的所有数据。

所以我想在用户滚动或用户查看 RecyclerView 的项目时调用数据

那么有没有什么方法可以在滚动时只调用前几个或 5 个 post 并调用其余的。

Activity XML-

 <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:id="@+id/tv_feed"
            android:text="Feed"
            android:layout_marginLeft="20dp"
            android:layout_below="@+id/rcv_news"
            android:layout_marginBottom="10dp"
            android:textColor="#000000"
            android:paddingLeft="10dp"
            android:textSize="30dp"
            android:fontFamily="@font/benchnine_bold" />

        <androidx.constraintlayout.widget.ConstraintLayout
            android:id="@+id/container"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@+id/tv_feed">

        </androidx.constraintlayout.widget.ConstraintLayout>

    </RelativeLayout>

片段XML-

<androidx.constraintlayout.widget.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@color/grey_bg"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <androidx.recyclerview.widget.RecyclerView
        android:id="@+id/post_ImagesRecyclerView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_margin="5dp"
        android:background="@color/grey_bg"
        android:nestedScrollingEnabled="false"/>

    <com.wang.avi.AVLoadingIndicatorView.......

Java -

postAdapter = new PostAdapter(context, false, false, postList, this, 1, loadType);
        LinearLayoutManager postListManager = new LinearLayoutManager(getActivity(), LinearLayoutManager.VERTICAL, false);
        post.setLayoutManager(postListManager);
        post.setAdapter(postAdapter);

        postPayload = new PostPayload(CommonData.getCampusId(), start, LIMIT, loadType, CommonData.getCreatorInfo());
        postPayload.setPostId(postId);
        if (postPayload.getLoadType() == Constants.LOAD_MY_POSTS)
            postPayload.setProfileCreatedBy(CommonData.getLoggedInUserId());
        postPayload.setSearchKey(searchKey);

        getPosts();
    }

    @Override
    public void getMorePost() {

        start = postList.size();
        postPayload.setStart(start);
        getPosts();
    }

使用 PagedListAdapter 并将 setPageSize 限制设置为 5 以了解详细实施阅读 https://proandroiddev.com/8-steps-to-implement-paging-library-in-android-d02500f7fffe

首先,即使调用了所有数据,recyclerview 也不会创建所有视图。它仅在需要时创建视图。所以你不需要担心延迟加载。

但是如果你想用滚动加载你的数据。您可以在您的片段 java class.I 中向您的 recyclerview 添加一个滚动更改侦听器,认为它会起作用。希望对你有帮助

    //load your desired numbers data to postList your (5 posts for this),  
    boolean isAlreadyLoading = false

                post.addOnScrollListener(new RecyclerView.OnScrollListener() {
                    @Override
                    public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int newState) {
                        super.onScrollStateChanged(recyclerView, newState);

                        if (postListManager != null) {


                             index = postManager.findFirstVisibleItemPosition();

                if(index > postList.size()-5 && !isAlreadyLoading)
                {
isAlreadyLoading=true;

        postAdapter.notifyDataSetChanged;
                }}}

loadNewData()
{
loadNewData(); //add new data to the end of the postList
isAlreadyLoading=false;
}