如何在滚动时调用RecyclerView项目

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

我在我的活动中在片段内部使用RecyclerView,下面的ConstraintLayout是片段的调用位置,当我打开应用程序时,它将调用RecyclerView的所有数据,这些数据未被用户打开。

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

所以有什么方法在滚动时只调用前几个或前5个帖子,然后调用其余的帖子。

活动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();
    }
java android android-studio adapter
2个回答
0
投票
首先,即使调用了所有数据,recyclerview也不会创建所有视图。它仅在需要时创建视图。因此,您无需担心延迟加载。

但是如果要通过滚动加载数据。您可以在片段Java类的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; }


0
投票
使用

PagedListAdapter并将setPageSize限制设置为5以进行详细实现,请阅读https://proandroiddev.com/8-steps-to-implement-paging-library-in-android-d02500f7fffe

© www.soinside.com 2019 - 2024. All rights reserved.