gpt4 book ai didi

java - 在recyclerview中打开项目后分页不起作用 - Android

转载 作者:行者123 更新时间:2023-12-05 00:02:34 28 4
gpt4 key购买 nike

我正在尝试将分页添加到 viewpager 内的 fragment (由 recyclerview 组成)。 完美运行 .
案例:
我单击任何项​​目(打开一个 Activity ,即 detailView),但在从 detailView 返回到包含 recyclerview 的 fragment 后,分页停止工作。
fragment 代码

private int pageNumber = 1;
在 oncreateview
   sharedPreferenceUtil.setStringPreference(Constants.TEMPPAGENO, "1");
onScroll 监听器:
    private RecyclerView.OnScrollListener recyclerViewOnScrollListener = new RecyclerView.OnScrollListener() {
@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
RecyclerView.LayoutManager layoutManager = recyclerView.getLayoutManager();
RecyclerView.Adapter adapter = recyclerView.getAdapter();
if (layoutManager.getChildCount() > 0) {
// Calculations..
int indexOfLastItemViewVisible = layoutManager.getChildCount() - 1;

View lastItemViewVisible = layoutManager.getChildAt(indexOfLastItemViewVisible);

int adapterPosition = layoutManager.getPosition(lastItemViewVisible);

boolean isLastItemVisible = (adapterPosition == adapter.getItemCount() - 1);

if (!postType.equals("bookmark") && isLastItemVisible && !isLoading && !allDone) {
pageNumber++;
discoverRecyclerAdapter.showLoading();
loadPosts();

sharedPreferenceUtil.setStringPreference(Constants.TEMPPAGENO, String.valueOf(pageNumber));

}
}

}
};
loadposts 函数
private void loadPosts() {

if (postType != null && mContext != null) {
if (postType.equals("bookmark")) {
new Handler().postDelayed(new Runnable() {
@Override
public void run() {
if (discoverRecyclerAdapter.isLoaderShowing())
discoverRecyclerAdapter.hideLoading();
if (swipeRefreshLayout.isRefreshing())
swipeRefreshLayout.setRefreshing(false);

discoverRecyclerAdapter.addItemsAtBottom(bookmarkedPosts);
if (bookmarkedPosts.isEmpty()) {
recyclerView.setVisibility(View.GONE);
emptyView.setVisibility(View.VISIBLE);
}
}
}, 200);
} else {
isLoading = true;
if (TextUtils.isEmpty(userId))
getPosts = weService.getPosts(sharedPreferenceUtil.getStringPreference(Constants.KEY_API_KEY, null), postType.equals("hot") ? 1 : 0, pageNumber);
else
getPosts = weService.getPostsByUserId(sharedPreferenceUtil.getStringPreference(Constants.KEY_API_KEY, null), userId, postType.equals("hot") ? 1 : 0, pageNumber);
getPosts.enqueue(callBack);
}
}
}
在 onResume ( fragment )
pageNumber = Integer.parseInt(sharedPreferenceUtil.getStringPreference(Constants.TEMPPAGENO, String.valueOf(pageNumber)));
pageNumber++;
discoverRecyclerAdapter.showLoading();
loadPosts();
OnStop( fragment )
@Override
public void onStop() {
super.onStop();
recyclerView.removeOnScrollListener(recyclerViewOnScrollListener);
if (getPosts != null && !getPosts.isCanceled())
getPosts.cancel();
}
开启 onPause( fragment )
@Override
public void onPause() {
super.onPause();
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(postEventReceiver);
LocalBroadcastManager.getInstance(mContext).unregisterReceiver(profileEventReceiver);
}

最佳答案

由于您要删除 onScrollListeneronStop您应该将其添加回 onStart()

@Override
public void onStart() {
recyclerView.addOnScrollListener(recyclerViewOnScrollListener);
...
}

关于java - 在recyclerview中打开项目后分页不起作用 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68857909/

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