我正在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);
}
答案 0 :(得分:0)
尝试下面的代码
// 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();
}
}
}
}
});
答案 1 :(得分:0)
在API url
中使用parameter
count
,每当调用getPagination()
方法时,您都可以在count
parameter
中发送增加的值。根据服务器端的count
参数,您可以添加逻辑以获取下一项。
查看如何使用截击在Api中传递参数
StringRequest strreq = new StringRequest(Request.Method.POST,
"http://196.1.1.1/myurl",
new Response.Listener<String>() {
@Override
public void onResponse(String Response) {
// get response
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError e) {
e.printStackTrace();
}
}){@Override
public Map<String, String> getParams(){
Map<String, String> params = new HashMap<>();
params.put("Count", count);
return mParams;
}
};
Volley.getInstance(this).addToRequestQueue(strreq);
希望这会有所帮助。
答案 2 :(得分:0)
您可以确定何时将回收站视图滚动到最后一个元素。然后,您可以发送下一页的请求。当下一页返回新结果时,将它们添加到上一个结果集中并更新列表。
How to implement pagination in RecyclerView on scroll