gpt4 book ai didi

java - 如何在滚动时调用 RecyclerView 项目

转载 作者:行者123 更新时间:2023-11-30 04:59:53 25 4
gpt4 key购买 nike

我在我的 Activity 中使用 fragment 内部的 RecyclerView,下面的 ConstraintLayout 是 fragment 被调用的地方,当我打开应用程序时,它会调用用户未打开的 RecyclerView 的所有数据。

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

那么有什么方法可以在滚动时只调用前几个或 5 个帖子,其余的则被调用。

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>

fragment 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();
}

最佳答案

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

但是如果你想用滚动加载你的数据。您可以在您的 fragment 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;
}

关于java - 如何在滚动时调用 RecyclerView 项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58425985/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com