1

How can I use the pagination library in the ArrayAdapter (I dont want to use ListView or RecyclerView)? categories is a list of string to be shown. I have a huge list of categories. I want it to be paginated. How to achieve this?

List<String> categories = getSomeCategories();
ArrayAdapter<String> adapter = new ArrayAdapter<String>(context, android.R.layout.select_dialog_item, categories);
autoCompleteTextView.setAdapter(adapter);

I have a method which returns the LiveData<PagedList<String> categories. I'm using the LivePagedListBuilder to build it. I want my adapter to observe on this.

Anup Ammanavar
  • 432
  • 1
  • 3
  • 16

1 Answers1

0

You can detect the scrolls using the recycler view's OnScrollListener, then check if the visible element is the last element in page, and if so load another page.

recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {

  @Override
  public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
    super.onScrolled(recyclerView, dx, dy);
    int visibleItemsCount = layoutManager.getChildCount();
    int totalItemsCount = layoutManager.getItemCount();
    int pastVisibleItems = layoutManager.findFirstVisibleItemPosition();
    int lastCompletelyVisibleItem = layoutManager.findLastCompletelyVisibleItemPosition();
    if (dy >= 0
        && (visibleItemsCount + pastVisibleItems) >= totalItemsCount
        && lastCompletelyVisibleItem == elements.size() - 1) {
      offset = elements.size();
      //Start at this offset
      populateRecyclerView(offset);
    }
  }
});
Murtadha S.
  • 442
  • 6
  • 15