如何在Android中将大型JSON数组拆分为页面 [英] How to split large json array into pages in Android
问题描述
我正在从 https://jsonplaceholder.typicode.com/photos 获取json数据,并希望将此数据拆分为页面,以便我可以在我的应用中实现Android Paging Library.有谁知道我怎么能做到这一点?
I'm fetching json data from https://jsonplaceholder.typicode.com/photos, and want to split this data into pages so that I can implement the Android Paging Library in my app. Does anyone know how I can achieve this?
推荐答案
对于分页,您将使用android jetpack https://developer.android.com/topic/libraries/architecture/paging
For paginatio you will use android jetpack https://developer.android.com/topic/libraries/architecture/paging
-
另一种方法
Another method
从api中获取特定数量的数据,然后将其添加到recyleview中,然后再次加载特定数据后执行以下操作,您将调用其他剩余的特定数据作为计算加载数据的计数器.
get specific number of data from api and add into recyleview after that do something like below after loading your specific data again you will call further remaining specific data make a counter for counting loaded data.
参考资料 https://www.youtube.com/watch?v = 2-vZ1g_G1Zo
boolean isScrolling=false;
rView.setAdapter(adapter);
rView.addOnScrollListener(new RecyclerView.OnScrollListener() {
@Override
public void onScrollStateChanged(@NonNull RecyclerView recyclerView, int
newState) {
super.onScrollStateChanged(recyclerView, newState);
if(newState == AbsListView.OnScrollListener.SCROLL_STATE_TOUCH_SCROLL)
{
isScrolling=true;
}
}
@Override
public void onScrolled(@NonNull RecyclerView recyclerView, int dx, int dy) {
super.onScrolled(recyclerView, dx, dy);
currentitems=layoutManager.getChildCount();
totaliitems=layoutManager.getItemCount();
scrolloutitems=layoutManager.findFirstVisibleItemPosition();
if(isScrolling && (currentitems + scrolloutitems == totaliitems))
{
isScrolling=false;
getAllItemList();
}
}
});
adapter.notifyDataSetChanged();
getAllItemList();
这篇关于如何在Android中将大型JSON数组拆分为页面的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!