8

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"
Anushka Khare
  • 363
  • 3
  • 11

2 Answers2

4

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 .

Anushka Khare
  • 363
  • 3
  • 11
  • Good find..!! But the rest of the items are not loading while scrolling the recyclerview.Any solutions? – kgandroid May 02 '20 at 21:10
2

EDIT2:

its showing the total no. of elements in my list rather than the pages size (10) that i have defined while creating the paged list 

Use setEnablePlaceholders(false)


Previous answer:

After wondering about this myself and reading through the comments and answers, I found out it has to with the config you set. More specifically the PageSize.

If you set it to 1, and scroll really fast, you'll see placeholders, meaning all the data isn't load yet.

From the documentation:

setPageSize

Defines the number of items loaded at once from the DataSource.

Should be several times the number of visible items onscreen.

Configuring your page size depends on how your data is being loaded and used. Smaller page sizes improve memory usage, latency, and avoid GC churn. Larger pages generally improve loading throughput, to a point (avoid loading more than 2MB from SQLite at once, since it incurs extra cost).

If you're loading data for very large, social-media style cards that take up most of a screen, and your database isn't a bottleneck, 10-20 may make sense. If you're displaying dozens of items in a tiled grid, which can present items during a scroll much more quickly, consider closer to 100.

P.S. Try not to use setInitialLoadSizeHint unless you really need it because it will run on the UI thread even if you've specified a fetchExecutor on a background thread.