The ListView and GridView have become native Android apps implemented in two of the most popular design patterns. At present, these patterns are used by a large number of developers, mainly because they are simple and straightforward implementations, while they provide a good, uncluttered user experience.
A common requirement for the ListView and GridView is that the component can still load more data dynamically as the user scrolls down continuously. This blog will lead you to achieve this feature in the ListView and GridView.
The main component We need is our Infinitescrolllistener class, which is inherited 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) {//Does 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 (Curre Ntpage + 1, totalitemcount); IsLoading = true; } }}
Attach the listener to the Adapterview Oncreateyourlistview.setonscrolllistener (new Infinitescrolllistener (5) { @Override public void loadmore (int page, int totalitemscount) { list
As above, we have used this class as an abstract class, which is a good design. Our Infinitescrolllistener class implements the Onscroll () method, but it does not implement Loadmore (), but it also implements the implementation class.
The Onscroll () method is called automatically when we scroll. Therefore, we recommend that in this method, do not carry out excessive processing and data calculation, because the scrolling is very frequent, this method call is also very frequent.
To achieve this, we just need to implement infinitescrolllistener and set this implementation class to Listview/gridview's Setonscrolllistener () method, just like the anonymous class of the second code fragment.
We implement the Infinitescrolllistener class, we also need to implement the Loadmore () method, in which we can add item to Listview/gridview and pass notifydatasetchanged () Notifies Listview/gridview that data has changed. Of course we can manually add local data, or L can load more data from a database or a restful service.
This is what we do in Android that can be infinitely scrollable in Listview/gridview. For a lot of information, Listview/gridview undoubtedly has a good source user experience.
Original: Http://www.avocarrot.com/blog/implement-infinitely-scrolling-list-android/