Android ListView Batch Loading + automatic loading (with source download)

Source: Internet
Author: User

Directly on the code, the code has comments:

public class Testforlistviewactivity extends Activity implementsonscrolllistener {private ListView mlistview = null; Private View mfooterview;private paginationadapter madapter;private Handler handler=new Handler ();p Rivate Boolean isloading;//indicates whether private final int max_count=35;//is being loaded, indicating that there is a total of max_count data on the server private final int each_count=10;// Represents the number of bars per load @overridepublic void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.main); Mfooterview = Getlayoutinflater (). Inflate (R.layout.loadmore, null); MListview = ( ListView) Findviewbyid (R.id.listview); Mlistview.addfooterview (Mfooterview);//Set list bottom view list<news> news=new Arraylist<news> (); madapter = new Paginationadapter (News); Mlistview.setadapter (madapter);//setting Setonscrolllistener automatically calls the Onscroll method. Mlistview.setonscrolllistener (this);} public void Onscroll (Abslistview view, int firstvisibleitem,int visibleitemcount, int totalitemcount) {if ( firstvisibleitem+visibleitemcount==totalitemcount&&!isloading) {//isloading = true means loading, loading complete setting isloading =false;isloading = true;//If there is data on the server, continue loading more, otherwise hide the bottom of the load more if ( Totalitemcount<=max_count) {//wait 2 seconds before loading, analog network wait Time is 2shandler.postdelayed (new Runnable () {public void run () { Loadmoredata ();}},2000);} Else{if (Mlistview.getfooterviewscount ()!=0) {Mlistview.removefooterview (Mfooterview);}}}} public void onscrollstatechanged (Abslistview arg0, int arg1) {log.i ("onscrollstatechanged", arg1+ "");} private void Loadmoredata () {int count = Madapter.getcount (); for (int i = 0; i < Each_count; i++) {if (Count+i<max_co UNT) {News item = new News (); Item.settitle ("Title" + (count+i)); Item.setcontent ("This is News Content" + (count+i)); madapt Er.addnewsitem (item);} Else{mlistview.removefooterview (Mfooterview);}} Madapter.notifydatasetchanged (); isloading = false;} Class Paginationadapter extends Baseadapter {list<news> newsitems;public paginationadapter (List<News> Newsitems) {this.newsitems = Newsitems;} public int GetCount () {return newsitems==nuLl?0:newsitems.size ();} Public Object getItem (int position) {return newsitems.get (position);} public long getitemid (int position) {return position;} public void Addnewsitem (News newsitem) {newsitems.add (NewsItem);} Public View GetView (int position, View Convertview, ViewGroup parent) {if (Convertview = = null) {Convertview = Getlayoutin Flater (). Inflate (r.layout.list_item,null);} News headlines TextView Tvtitle = (TextView) Convertview.findviewbyid (r.id.newstitle); Tvtitle.settext (Newsitems.get ( Position). GetTitle ());//news content TextView Tvcontent = (TextView) Convertview.findviewbyid (r.id.newscontent); Tvcontent.settext (Newsitems.get (position). GetContent ()); return Convertview;}}}


Demo source download: Source download


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.