Android Data batch loading-swipe to bottom auto load list

Source: Internet
Author: User
Tags addall

Android Data batch loading-swipe to bottom auto load list


May 9, 2014

This blog post describes how to do data batch loading, in the application development will be used to the ListView, click More load data is we often simple, in order to provide the user experience, when the user scrolls the list to the bottom of the automatic loading of data, this form is used more.

Here is an example of how to simulate 20 data each time, swipe to the bottom and then request 20 data until the request to limit the number of pages

Specific code implementation:

/08_datapageload/src/com/wwj/datapageload/mainactivity.java

Package Com.wwj.datapageload;import Java.util.arraylist;import Java.util.list;import android.app.activity;import Android.os.bundle;import Android.os.handler;import Android.util.log;import Android.view.view;import Android.widget.abslistview;import Android.widget.abslistview.onscrolllistener;import Android.widget.ArrayAdapter Import Android.widget.listview;public class Mainactivity extends Activity {private ListView listview;private list< string> data = new arraylist<string> (); Arrayadapter<string> adapter; View footer; @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); footer = Getlayoutinflater (). Inflate (r.layout.footer, null); ListView = ( ListView) Findviewbyid (R.id.listview), Listview.setonscrolllistener (New Scrolllistener ());//Analog Data Data.addall ( Dataservice.getdata (0)); adapter = new Arrayadapter<string> (this, R.layout.listview_item,r.id.textview, data); Listview.addfooterview (FooteR);//Add footer (placed in ListView last) Listview.setadapter (adapter); Listview.removefooterview (footer);} private int number = 20; How many data is fetched each time private int maxpage = 5; Total number of pages private Boolean loadfinish = true; Indicates whether the data is loaded complete private final class Scrolllistener implements Onscrolllistener {@Overridepublic void onscroll (Abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {log.i ("mainactivity", "Onscroll" ( Firstvisibleitem= "+ Firstvisibleitem +", visibleitemcount= "+ VisibleItemCount +", totalitemcount= "+ totalItemCount+") "); final int loadtotal = Totalitemcount;int Lastitemid = listview.getlastvisibleposition (); Gets the last item of the current screen idif ((lastitemid + 1) = = Totalitemcount) {//To reach the last record of the data if (Totalitemcount > 0) {//current page int Currentpag E = totalitemcount% Number = = 0? Totalitemcount/number:totalitemcount/number + 1;int nextpage = currentpage + 1; Next if (nextpage <= maxpage && loadfinish) {loadfinish = False;listview.addfooterview (footer);// Open a line loads load data new Thread (new Runnable () {@Overridepublic void run () {try {thread.sleep (+);} catch (Interruptedexception e) {e.printstacktrace ();} list<string> result = Dataservice.getdata (loadtotal, number);//Send Message Handler.sendmessage (handler.obtainmessage (100,data));}). Start ();}}} @Overridepublic void onscrollstatechanged (abslistview view, int scrollstate) {log.i ("mainactivity", " Onscrollstatechanged (scrollstate= "+ scrollstate +") ");}} Private Handler Handler = new Handler () {public void Handlemessage (Android.os.Message msg) {Data.addall (list<string& Msg.obj);//Tell the ListView that the data has changed, requiring the ListView update interface to display adapter.notifydatasetchanged (); if ( Listview.getfooterviewscount () > 0) {//If there is a bottom view listview.removefooterview (footer);} Loadfinish = true; Load complete};};}

/08_datapageload/src/com/wwj/datapageload/dataservice.java

Package Com.wwj.datapageload;import Java.util.arraylist;import Java.util.list;public class DataService {public static list<string> getData (int offset, int maxresult) {//paging limit//0,20list<string> data = new arraylist<string > (); for (int i = 0; i < i++) {Data.add ("batch loading of ListView data" + i);} return data;}}


As follows:


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.