androiddesign-patternsmvvmpagedlistandroid-paging-library

How to get pagedlist size in android when using paging library with mvvm


please, I'm using android MVVM with retrofit and with paging library and the problem is I can't get size of pagedlist. pagedlist = 0 in UI always.

and the below code that the way how I get the list size.

 private void getAllAds() {
        userHomeViewModel.getHomeAllAdsPagedList().observe(this, new Observer<PagedList<AllAdsModel>>() {
            @Override
            public void onChanged(PagedList<AllAdsModel> list) {
                allAdsModelPagedList = list;
                Toast.makeText(AllAdsActivity.this, ""+list.size(), Toast.LENGTH_SHORT).show();
                //list.size = 0!!!!
                initUserAllAds();
            }
        });
    }

Solution

  • PagedList is loaded incrementally, so its size will change overtime and will always be 0 (with placeholders disabled) before calling submitList, and it has been given a chance to load some items.

    You should instead look to take advantage of the APIs on PagedListAdapter.

    If you're interested in the current size of the currently presented list, you can use PagedListAdapter.currentList.size

    If you want to be notified when a new PagedList is presented, you can use PagedListAdapter.onCurrentListChanged. This method is called after initial load and diffutil runs, so it is stnchronous with when the new PagedList is presented.

    Note that PagedList.size includes placeholders. In the case where you have placeholdersEnabled, but you're only interested in the loaded items you should use PagedList.loadedCount.