gpt4 book ai didi

android - 如何在recyclerview中实现分页?

转载 作者:行者123 更新时间:2023-11-29 02:27:09 28 4
gpt4 key购买 nike

我正在 recyclerView 中执行分页。我的数据加载成功,问题是如何向服务器请求下一页。我如何执行分页以及如何找到正在滚动的页码。

我的代码是:

rvLatestProduct.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
//super.onScrolled(recyclerView, dx, dy);

visibleItemCount = rvLatestProduct.getChildCount();
totalItemCount = linearLayoutManager.getItemCount();
firstVisibleItem = linearLayoutManager.findFirstVisibleItemPosition();
Log.e("totalItemCount",String.valueOf(totalItemCount));
if (loading) {
if (totalItemCount > previousTotal) {
loading = false;
// currentPage += 1;
previousTotal = totalItemCount;
Log.e("previousTotal",String.valueOf(previousTotal));
}
}
if (!loading && (totalItemCount - visibleItemCount)
<= (firstVisibleItem + visibleThreshold)) {
//int initialSize = totalItemCount.size;
getPagination();
//val updatedSize = dataList.size
// recyclerView.post { adapter.notifyItemRangeInserted(initialSize, updatedSize) }
loading = true;
}
}
});

和getPagination();代码为

  private void getPagination(){
final JsonArrayRequest request = new JsonArrayRequest(Request.Method.GET,
AllURLs.viewAllLatestProduct,null,
new Response.Listener<JSONArray>() {
JSONArray object;
@Override
public void onResponse(JSONArray response) {
try {
for (int i=0;i<response.length();i++) {
object = response.getJSONArray(i);
}
setLatestProductAdapter(object);
adapter.notifyDataSetChanged();
// Toast.makeText(context,"page no" + pageNo,Toast.LENGTH_SHORT).show();
} catch (JSONException e) {
e.printStackTrace();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {

}
});
VolleySingleton.getInstance(context).addToRequestQueue(request);
}

最佳答案

试试下面的代码

// Define variable in class 

private int page = 1, limit = 15;
private int firstVisibleItem, visibleItemCount, totalItemCount;
private int previousTotal = 0;
private int visibleThreshold = 10;

recyclerView 滚动监听器代码:

recyclerView
.addOnScrollListener(new RecyclerView.OnScrollListener() {

@Override
public void onScrollStateChanged(RecyclerView recyclerView,
int newState) {
super.onScrollStateChanged(recyclerView, newState);

}

@Override
public void onScrolled(RecyclerView recyclerView, int dx,
int dy) {
super.onScrolled(recyclerView, dx, dy);
if (dy > 0) {
mLinearLayoutManager = (LinearLayoutManager) recyclerLikedItemsList
.getLayoutManager();

if (!CheckInternet
.checkInternetConnection(AllLikedItemList.this)) {
//show connection error


} else {

visibleItemCount = recyclerView.getChildCount();
totalItemCount = mLinearLayoutManager
.getItemCount();
firstVisibleItem = mLinearLayoutManager
.findFirstVisibleItemPosition();

if (loading) {
if (totalItemCount > previousTotal) {

previousTotal = totalItemCount;
page++;
}
}
if (!loading
&& (firstVisibleItem + visibleThreshold + visibleItemCount) >= totalItemCount) {
page++;
// call pagination and pass page limit
getPagination();

}
}

}
}
});

关于android - 如何在recyclerview中实现分页?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51558823/

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