Private ListView LV;
Adapter Data collection
Private list<string> List = new arraylist<string> ();
Adapter
Private arrayadapter<string> adapter;
Determine whether to reach the low end
Private Boolean Isbottom;
@Override
protected void OnCreate (Bundle savedinstancestate) {
Super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
LV = (ListView) Findviewbyid (r.id.lv);
InitData ();
Creating an Adapter Object
adapter = new Arrayadapter<string> (This,
Android. R.layout.simple_list_item_1, list);
Setting up the adapter
Lv.setadapter (adapter);
Lv.setonscrolllistener (New Onscrolllistener () {
View:listview
There are 3 states of scrollstate: Stop Scroll_state_idle
Touch scrolling: Scroll_state_touch_scroll Quick slide: scroll_state_fling
@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
TODO auto-generated Method Stub
Switch (scrollstate) {
Quick Slide
Case scroll_state_fling:
LOG.I ("onscrollstatechanged", "quick slide");
Break
Stop it
Case Scroll_state_idle:
LOG.I ("onscrollstatechanged", "no sliding");
if (Isbottom) {
Loading the next page of data
Myasynctask task = new Myasynctask ();
Task.execute ();
Change
Isbottom = false;
}
Break
Touch scrolling
Case Scroll_state_touch_scroll:
LOG.I ("onscrollstatechanged", "Touch swipe");
Break
}
}
@Override
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
TODO auto-generated Method Stub
LOG.I ("Onscroll", "firstvisibleitem----->" + firstvisibleitem);
LOG.I ("Onscroll", "visibleitemcount----->" + visibleitemcount);
LOG.I ("Onscroll", "totalitemcount----->" + totalitemcount);
Determine if the index value of the first visible item item + the number of item on the page = = The total number of items loaded this time---"reaches the low end
if (firstvisibleitem + visibleitemcount = = Totalitemcount) {
Isbottom = true;
}
}
});
}
Initializing data
private void InitData () {
for (int i = 0; i <; i++) {
List.add ("Chinese Cabbage price u disk" + i);
}
}
To open an asynchronous task load new data
public class Myasynctask extends Asynctask<void, Void, list<string>> {
Private ProgressDialog Dialog;
Initialize the Progress box
@Override
protected void OnPreExecute () {
TODO auto-generated Method Stub
Super.onpreexecute ();
dialog = new ProgressDialog (mainactivity.this);
Dialog.setmessage ("Please later ...");
Dialog.show ();
}
Simulate background load data
@Override
Protected list<string> doinbackground (Void ... params) {
list<string> list = new arraylist<string> ();
for (int i = 0; i <; i++) {
List.add ("Chinese cabbage Sale" + i);
try {
Thread.Sleep (500);
} catch (Interruptedexception e) {
TODO auto-generated Catch block
E.printstacktrace ();
}
}
Returns the newly loaded data
return list;
}
Get the Data Update adapter
@Override
protected void OnPostExecute (list<string> result) {
TODO auto-generated Method Stub
Super.onpostexecute (result);
dialog box disappears
Dialog.dismiss ();
Judge
if (result! = null) {
To add data to the collection of adapters
List.addall (result);
}
Adapter.notifydatasetchanged ();
}
}
Custom ListView Pull-up load more