Custom ListView Pull-up load more

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.