Android: Pagination in Custom List View
Points To Remember
You can refer to blog - Create list view with custom list view adapter.Now we will implement pagination in this list view.
Pagination in Custom List View
- We will add 20 items each time to the list view.
- After 18th element is displayed on the screen we will call a method to load new items to the list view, so that user does not wait for new items to be loaded.(In real cases you might be loading from a server and this might take much time).
- We will restrict our pagination after 400 elements have been displayed.( It is a replication of the scenario when the server has no more items to be displayed).
We will be setting a onScrollListener() on the listView to check when to load new items and add them to the list.
listView.setOnScrollListener(new AbsListView.OnScrollListener() { @Override public void onScrollStateChanged(AbsListView absListView, int i) { } @Override public void onScroll(AbsListView absListView, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if(firstVisibleItem+visibleItemCount > totalItemCount-2 && totalItemCount < maxRecords){ personAdapter.add(bootData()); personAdapter.notifyDataSetChanged(); } } });
No comments: