Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

I am using android paging library but rather than getting data in chunks I am getting complete list in one go

ViewModel class inside which we are loading pagedlist using data source.

    public class RecipeListViewModel extends ViewModel {

    public LiveData<PagedList<RecipeListPojo>> mutableLiveData;

    public void init(RecipeFrom recipeFrom, RecipeDao recipeDao) {
    mutableLiveData = new 
    LivePagedListBuilder(recipeDao.getRecipeList(),10).build();
   }
   }

This is my dao in which we are fetching data in form of datasource factory.

   @Dao
   public interface RecipeDao {
   @Query("select * from recipe")
   public DataSource.Factory<Integer, RecipeListPojo> getRecipeList();
   }

Inside my RecipeListPojo I have created the DiffCallBack.

   public static DiffUtil.ItemCallback<RecipeListPojo> diffCallback=new 
   DiffUtil.ItemCallback<RecipeListPojo>() {
   @Override
   public boolean areItemsTheSame(RecipeListPojo oldItem, RecipeListPojo 
   newItem) {
   return oldItem.getId()==newItem.getId();
   }

   @Override
   public boolean areContentsTheSame(RecipeListPojo oldItem, RecipeListPojo 
   newItem) {
   return oldItem.equals(newItem);
   }
   };

Inside my activity i am reciveing the pagedlist through observer and setting my adapter.

  arrayListObserver=new Observer<PagedList<RecipeListPojo>>() {
  @Override
  public void onChanged(@Nullable PagedList<RecipeListPojo> recipePojos) {

  if(recipePojos!=null)
  {
  recipeAdapter.submitList(recipePojos);
  recyclerView.setAdapter(recipeAdapter);
  progressBar.setVisibility(GONE);
  }
  }
  };
  recipeFrom=new RecipeFrom.RecipeFromBuilder(fromActivity).build();
  recipeDao=GlobalApplication.recipeRoomDatabase.getRecipeDao();
  recipeListViewModel.init(recipeFrom,recipeDao);
  recipeListViewModel.mutableLiveData.observe(this,arrayListObserver);

This is how my adapter looks like.

  public class RecipeListAdapter extends 
  PagedListAdapter<RecipeListPojo,RecipeListAdapter.RecipeListHolder> {

  private LayoutInflater inflater;
  private Context context;

  public RecipeListAdapter()
  {
  super(RecipeListPojo.diffCallback);
  }

  @Override
  public RecipeListHolder onCreateViewHolder(ViewGroup parent, int viewType) 
  {

  context=parent.getContext();
  inflater=LayoutInflater.from(context);
  View rootView = inflater.inflate(R.layout.one_item_recipe_list, null, 
  false);
  return new RecipeListHolder(rootView);
  }

  @Override
  public int getItemCount() {
  return super.getItemCount();
  }
  }

Library used-

  // Paging
  implementation "android.arch.paging:runtime:1.0.0-rc1"
like image 393
Anushka Khare Avatar asked May 02 '18 06:05

Anushka Khare


People also ask

How Paging library works in android?

The Paging Library lets you load data directly from your backend using keys that the network provides. Your data can be uncountably large. Using the Paging Library, you can load data into pages until there isn't any data remaining. You can observe your data more easily.

How do I use PagedList on Android?

Get the item in the list of loaded items at the provided index. Return the Config used to construct this PagedList. Return the DataSource that provides data to this PagedList. Return the key for the position passed most recently to loadAround(int) .

What is jetpack paging?

The Paging library helps you load and display pages of data from a larger dataset from local storage or over network. This approach allows your app to use both network bandwidth and system resources more efficiently.

What is kotlin pagination?

We can use the paging 3 library for pagination. Because it provides first-class Kotlin support to handle easy or complex data operations like data loading from network, database or combination of different data sources. And the paging 3 library can also support Kotlin coroutine flow.


1 Answers

As per my research, I have finally found that It returns a list of size = total no. of items, but only pagedList size * 3 will be initialized and rest items will be nulls, and they will be updated while scrolling the recyclerView using PagedListAdapter .

like image 160
Anushka Khare Avatar answered Oct 03 '22 19:10

Anushka Khare