ListView is one of the most commonly used controls in Android.
In the actual application will often encounter a one-time load of all the data too much, the need for paging load to increase the efficiency of the program!
This demo is to put the updated listening in the ListView foot, through the bottom button click Trigger or through the sliding event monitoring, when sliding to the bottom of the list to trigger.
There is a picture of the true image:
List Initial state
Trigger load
After loading
Part of the code:
Mainactivity
Copy Code code as follows:
private void Initactivity () {
TODO auto-generated Method Stub
Loadmoreview = Getlayoutinflater (). Inflate (R.layout.loadmore, NULL);
LOADMORE_BTN = (Button) Loadmoreview.findviewbyid (R.ID.LOADMORE_BTN);
Loadmore_btn.setonclicklistener (New Onclicklistener () {
@Override
public void OnClick (View v) {
TODO auto-generated Method Stub
DoUpdate ();
}
});
ListView = (ListView) Findviewbyid (R.id.listview);
Listview.addfooterview (Loadmoreview);
Initadapter ();
Listview.setadapter (adapter);
Listview.setonscrolllistener (New Onscrolllistener () {
@Override
public void onscrollstatechanged (Abslistview view, int scrollstate) {
TODO auto-generated Method Stub
int itemslastindex = Adapter.getcount ()-1;
int lastindex = Itemslastindex + 1;
if (scrollstate = = Onscrolllistener.scroll_state_idle
&& Visiblelast = = lastindex) {
Can be automatically loaded when you add a slide to the bottom of the list
DoUpdate ();
}
}
@Override
public void Onscroll (Abslistview view, int firstvisibleitem,
int visibleitemcount, int totalitemcount) {
TODO auto-generated Method Stub
Visiblecount = VisibleItemCount;
Visiblelast = Firstvisibleitem + visibleItemCount-1;
}
});
}
protected void DoUpdate () {
TODO auto-generated Method Stub
Loadmore_btn.settext ("Loading ...");
Handler.postdelayed (New Runnable () {
@Override
public void Run () {
TODO auto-generated Method Stub
LoadData ();
Adapter.notifydatasetchanged ()//notification adapter data changes
Listview.setselection (Visiblelast-visiblecount + 1);
Loadmore_btn.settext ("Load more");
}
}, 2000);
}
private void LoadData () {
TODO auto-generated Method Stub
int count = Adapter.getcount ();
for (int i = count; i < count +; i++) {
Adapter.additem (string.valueof (i + 1));
}
}
private void Initadapter () {
TODO auto-generated Method Stub
list<string> items = new arraylist<string> ();
for (int i = 0; i < i++) {
Items.Add (string.valueof (i + 1));
}
adapter = new ListAdapter (this, items);
}
@Override
public boolean Oncreateoptionsmenu (Menu menu) {
Inflate the menu; This adds items to the action bar if it is present.
Getmenuinflater (). Inflate (R.menu.activity_main, menu);
return true;
}
ListAdapter
Copy Code code as follows:
Public ListAdapter (context, list<string> items) {
This.items = items;
This.inflater = (layoutinflater) context
. Getsystemservice (Context.layout_inflater_service);
}
@Override
public int GetCount () {
TODO auto-generated Method Stub
return Items.size ();
}
@Override
Public Object getitem (int position) {
TODO auto-generated Method Stub
return Items.get (position);
}
@Override
public long getitemid (int position) {
TODO auto-generated Method Stub
return position;
}
@Override
Public View GetView (int position, View Convertview, ViewGroup parent) {
TODO auto-generated Method Stub
if (Convertview = = null) {
Convertview = inflater.inflate (R.layout.list_item, NULL);
}
TextView TextView = (TextView) convertview
. Findviewbyid (R.id.list_item_text);
Textview.settext (Items.get (position));
return convertview;
}
/**
* Add list items
* @param Item
*/
public void AddItem (String item) {
Items.Add (item);
}
Remind: To download Android-support-v4.jar This third party jar bag Oh! Download is not available here!