如何在Android中将大型json数组拆分为页面 [英] How to split large json array into pages in Android

查看:31
本文介绍了如何在Android中将大型json数组拆分为页面的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从 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 jetpackhttps://developer.android.com/topic/libraries/architecture/paging

  1. 另一种方法

  1. 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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆