1. Introduction:
In order to improve the efficiency of the ListView and the performance of the application, in the Android application should not load all the information to be displayed in the ListView, but instead take the batch loading strategy, with the user sliding, dynamically loading the required data from the background, and render to the ListView component, which can greatly improve the performance and user experience of the application.
2. Interaction:
Into the ListView component, first preload N records, when the user slides to the last record to display the load prompt information, and loads n data from the background, then renders the UI interface.
3.:
4. Program implementation:
Package Com.focus.loading;import Android.app.listactivity;import Android.os.bundle;import Android.os.Handler; Import Android.view.gravity;import Android.view.view;import Android.view.viewgroup;import Android.widget.abslistview;import Android.widget.baseadapter;import Android.widget.linearlayout;import Android.widget.listview;import Android.widget.progressbar;import Android.widget.textview;import Android.widget.abslistview.onscrolllistener;import Android.widget.linearlayout.layoutparams;public Class Listviewloadingactivity extends listactivity implementsonscrolllistener {@Overridepublic void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate);/** * Add-ons layout, this layout is added to the footer of the ListView. */mloadlayout = new LinearLayout (this); mloadlayout.setminimumheight; mloadlayout.setgravity (Gravity.CENTER); Mloadlayout.setorientation (linearlayout.horizontal);/** * Adds a round progress bar to the add-ons layout. */progressbar Mprogressbar = new ProgressBar (this); mprogressbar.setpadding (0, 0, 0); Mloadlayout.addView (Mprogressbar, mprogressbarlayoutparams);/** * Adds a hint to the add-ons layout. */textview mtipcontent = new TextView (this); Mtipcontent.settext ("Loading ..."); Mloadlayout.addview (Mtipcontent, MTIPCONTENTLAYOUTPARAMS);/** * Gets the ListView component and adds the add-in layout to the footer of the ListView component. */mlistview = Getlistview (); Mlistview.addfooterview (mloadlayout);/** * Group The ListView component sets adapter and sets the slide listener event. */setlistadapter (Mlistviewadapter); Mlistview.setonscrolllistener (this);} public void Onscroll (Abslistview view, int mfirstvisibleitem,int mvisibleitemcount, int mtotalitemcount) {Mlastitem = MFi Rstvisibleitem + mvisibleitemcount-1;if (Mlistviewadapter.count > MCount) {mlistview.removefooterview (mLoadLayout );}} public void onscrollstatechanged (Abslistview view, int mscrollstate) {/** * When the ListView is sliding to the last record, we will see that it has been added to the ListView Add-ons layout, the rest of the data should be loaded. */if (Mlastitem = = mlistviewadapter.count&& Mscrollstate = = Onscrolllistener.scroll_state_idle) {if ( Mlistviewadapter.count <= mCount) {mhandler.postdelayed (new Runnable () {@Overridepublic void Run () {Mlistviewadapter.count + = 10;mlistviewadapter.notifydatasetchanged (); Mlistview.setselection ( Mlastitem);}}, 1000);}}} Class Listviewadapter extends Baseadapter {int count = 10;public int GetCount () {return count;} Public Object getItem (int position) {return position;} public long getitemid (int position) {return position;} Public View GetView (int position, view view, ViewGroup parent) {TextView mtextview;if (View = = null) {Mtextview = new Text View (listviewloadingactivity.this);} else {Mtextview = (TextView) view;} Mtextview.settext ("Item" + position); Mtextview.settextsize (20f); mtextview.setgravity (Gravity.center); Mtextview.setheight (); return mtextview;}} Private LinearLayout mloadlayout;private ListView mlistview;private listviewadapter mlistviewadapter = new Listviewadapter ();p rivate int mlastitem = 0;private int mCount = 41;private final Handler mhandler = new Handler ();p rivate Final Layoutparams mprogressbarlayoutparams = new Linearlayout.layoutparams (linearlayout.layoutparams.wrap_content,linearlayout.layoutparams.wrap_content);p rivate final Layoutparams mTipContentLayoutParams = new Linearlayout.layoutparams (linearlayout.layoutparams.wrap_content,linearlayout.layoutparams.wrap_content);}
The listview dynamically loads data in Android