Transferred from: http://blog.csdn.net/shineflowers/article/details/41744241
In Android there are many times will choose to load data with the ListView, some batch loading, such as loading 20, 100 will be loaded 5 times, if a one-time loading 100, the ListView load will be slow, if there is a picture, a waste of traffic, Second, the item in the picture will appear dislocation of the problem L,listview loading data current practice of many apps
1. Load in batches, swipe to the bottom for automatic Updates
2. Swipe to the bottom, manually click to load more
3. Drop-down refresh + Bottom load more
Realize the effect of the ListView sliding to the bottom of the automatic update today.
Principle: Monitor the sliding event of the ListView, determine if the ListView is sliding to the bottom, and then load the data.
Public classMainactivity extends Activity {PrivateListView ListView; PrivateList<string>datas; PrivateLayoutinflater Inflater; PrivateMyadapter Adapter; PrivateList<string>contents; Private intCount =0; PrivateView Footview; PrivateHandler Handler =NewHandler (); intLastItem; Privaterelativelayout loading; @Overrideprotected voidonCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); InitData (); Inflater= Layoutinflater. from( This); ListView=(ListView) Findviewbyid (R.id.listview); Adapter=NewMyadapter (); Footview= Inflater.inflate (R.layout.footer,NULL); Loading=(relativelayout) Footview.findviewbyid (r.id.loading); //ListView Addfooterview () Add view to the bottom of the ListView must be added to Listview.setadapter (adapter); This code precedesListview.addfooterview (Footview); Listview.setadapter (adapter); //Add ListView Scrolling ListenerListview.setonscrolllistener (NewOnscrolllistener () {//Abslistview View This view object is the ListView@Override Public voidOnscrollstatechanged (Abslistview view,intscrollstate) { if(Scrollstate = =onscrolllistener.scroll_state_idle) { if(view.getlastvisibleposition () = = View.getcount ()-1) {loaddata (); } }} @Override Public voidOnscroll (Abslistview view,intFirstvisibleitem,intVisibleItemCount,intTotalitemcount) {LastItem= Firstvisibleitem + VisibleItemCount-1 ; } }); } protected voidLoadData () {loading.setvisibility (view.visible); Handler.postdelayed (NewRunnable () {@Override Public voidrun () {load (); Loading.setvisibility (View.gone); Adapter.notifydatasetchanged (); } }, the); } protected voidload () {intCount=adapter.getcount () +1; for(inti=count;i<count+ -; i++) {Contents.add ("Loading Data: ::"+i); } } Private voidInitData () {contents=NewArraylist<string>(); for(intI=1;i< -; i++) {Contents.add ("Loading Data: ::"+i); } } classMyadapter extends baseadapter{@Override Public intGetCount () {returncontents.size (); } @Override PublicObject GetItem (intposition) { returnContents.Get(position); } @Override Public LongGetitemid (intposition) { returnposition; } @Override PublicView GetView (intposition, View Convertview, ViewGroup parent) {Viewholder holder; if(convertview==NULL) {Convertview= Inflater.inflate (R.layout.item, parent,false); Holder=NewViewholder (); Holder.tvcontent=(TextView) Convertview.findviewbyid (r.id.tvcontent); Convertview.settag (holder); }Else{Holder=(Viewholder) Convertview.gettag (); } holder.tvContent.setText (contents.Get(position)); returnConvertview; } classviewholder{TextView tvcontent; } }}
Go ListView Scroll to the bottom to automatically load data