When too much data needs to be displayed on the listview, all the data will be loaded at once, and the interface will be stuck, which will affect the user experience. In this case, the loading should be completed several times.
To implement this function, an onscrolllistener interface and a footview layout file are required.
1. The layout file of footview is very simple, which indicates loading.
<? XML version = "1.0" encoding = "UTF-8"?> <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: gravity = "center_horizontal" Android: orientation = "vertical"> <textview Android: layout_width = "match_parent" Android: layout_height = "match_parent" Android: text = "load more"/> </linearlayout>
At this time, when I call listview. removefootview (View);, an error occurs. After finding the cause, I finally understand
Before using the set adapter class, you must first use listview. addfooterview (footerview );
Because listview creates different adapter classes based on whether footview exists to view the source code
public void setAdapter(ListAdapter adapter) { if (mHeaderViewInfos.size() > 0|| mFooterViewInfos.size() > 0) { mAdapter = new HeaderViewListAdapter(mHeaderViewInfos, mFooterViewInfos, adapter); } else { mAdapter = adapter; }}
No addfootview is provided before the set method. If removefootview is called, a class Exception error will be reported.
2. Create a sliding listener class
Private class implements detail {private Boolean addflag; @ overridepublic void detail (abslistview view, int scrollstate) {}@ overridepublic void onscroll (abslistview view, int detail, int visibleitemcount, int totalitemcount) {If (totalitemcount = visibleitemcount + firstvisibleitem & firstvisibleitem! = 0 &&! Addflag) {// todo load the new data item // if there is data, addflag = true; so that the next time you can enter the if statement to load the listview. setselection (totalItemCount-1); // display the new add-on in listview }}}
According to the practice, you do not need to add footview because data is pulled from the database because it is fast and can hardly be seen.