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