ListView and GridView have become two of the most popular design modes in the native Android Application implementation. Currently, these models are used by a large number of developers mainly because they are simple and direct implementation, and they provide a good and clean user experience.
A common requirement for ListView and GridView is that the component can dynamically load more data while the user keeps scrolling down. This blog will show you how to implement this function in ListView and GridView.
The main component we need is our InfiniteScrollListener class, which inherits from OnScrollListener. Let's take a look at the specific code implementation:
public abstract class InfiniteScrollListener implements AbsListView.OnScrollListener { private int bufferItemCount = 10; private int currentPage = 0; private int itemCount = 0; private boolean isLoading = true; public InfiniteScrollListener(int bufferItemCount) { this.bufferItemCount = bufferItemCount; } public abstract void loadMore(int page, int totalItemsCount); @Override public void onScrollStateChanged(AbsListView view, int scrollState) { // Do Nothing } @Override public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) { if (totalItemCount < itemCount) { this.itemCount = totalItemCount; if (totalItemCount == 0) { this.isLoading = true; } } if (isLoading && (totalItemCount > itemCount)) { isLoading = false; itemCount = totalItemCount; currentPage++; } if (!isLoading && (totalItemCount - visibleItemCount)<=(firstVisibleItem + bufferItemCount)) { loadMore(currentPage + 1, totalItemCount); isLoading = true; } }}
// Attach the listener to the AdapterView onCreateyourListView.setOnScrollListener(new InfiniteScrollListener(5) { @Override public void loadMore(int page, int totalItemsCount) { List<HashMap<String, String>> newData = loader.loadData(); dataList.addAll(newData); adapter.notifyDataSetChanged(); }});
As above, we have taken this class as an abstract class, which is a good design. Our InfiniteScrollListener class implements the onScroll () method, but does not implement loadMore (), but implements it for the Implementation class.
The onScroll () method is automatically called when we scroll. Therefore, we recommend that you do not perform excessive processing or data computing in this method, because scrolling is frequent and this method is frequently called.
To achieve this, we only need to implement InfiniteScrollListener and set this implementation class to the setOnScrollListener () method of ListView/GridView, just like the Anonymous class of the second code snippet.
We have implemented the InfiniteScrollListener class, and we also need to implement the loadMore () method. In this method, we can add an Item to ListView/GridView and use policydatasetchanged () notifies ListView/GridView that the data has changed. Of course, you can manually add local data or Load more data from the database or RESTful service.
This is what we implement in Android that the ListView/GridView can be infinitely rolled. For a large amount of information, ListView/GridView undoubtedly has a good source user experience.
Original article: http://www.avocarrot.com/blog/implement-infinitely-scrolling-list-android/