<span id="Label3"></p><p><p>The previous time to do Sina Weibo project has been to achieve the ListView page loading data, today finally realized, haha! Feel very good, and today wrote a demo for everyone to share under.</p></p><p><p>First of all, the optimization of the listview, which is also the interview frequently test Topics. There are three ways to optimize the scheme: 1, assuming that you define the adapter, then infer whether Contentview is empty in the GetView method, assume null to create the Contentview and return, assuming that the Contentview is not returned directly. This allows you to create as few view as Possible. 2. Set tag to Contentview and pass in a Viewhoder object to cache the data to be Implemented. 3, assuming that there are too many item displays in the listview, consider paging loading.</p></p><p><p>Here's a look at paging loading Data. First create the Listview.xml under Layout:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" match_parent " android:layout_height=" match_parent " android:o rientation= "vertical" > <listview android:id= "@+id/listview1" android:layout_width= "match_ Parent " android:layout_height=" wrap_content "> </ListView></LinearLayout></pre></pre>Then create the Listview_item.xml:<p><p></p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:orientation=" vertical " android:layout_width=" fill_parent " android:layout_height=" Fill_parent "> <textview android:id=" @+id/list_item_text " android:layout_width=" fill_parent " android:layout_height= "fill_parent" android:gravity= "center" android:textsize= "20sp" android: paddingtop= "10dp" android:paddingbottom= "10dp"/> </LinearLayout> </pre></pre><br><p><p>Then add an XML to the multiple button:</p></p><p><p></p></p><pre name="code" class="html"><pre name="code" class="html"><?xml version= "1.0" encoding= "utf-8"? ><linearlayout xmlns:android= "http://schemas.android.com/apk/res/ Android " android:layout_width=" fill_parent " android:layout_height=" wrap_content " android:gravity=" Center " android:orientation=" Vertical "> <button android:id=" @+id/loadmorebutton " Android : layout_width= "fill_parent" android:layout_height= "wrap_content" android:onclick= "loadmore " android:text= "loads Many other"/></linearlayout></pre></pre><br><p><p></p></p><p><p>Code section:</p></p><p><p></p></p><pre name="code" class="html">public class Listviewadapter extends Baseadapter {private static map<integer,view> m=new hashmap<integer,view& gt; ();p rivate list<string> items;private layoutinflater inflater;public listviewadapter (List<String> items, context Context) {super (); this.items = Items;this.inflater = (layoutinflater) Context.getsystemservice ( context.layout_inflater_service);} @Overridepublic int GetCount () {//TODO auto-generated method Stubreturn items.size ();} @Overridepublic Object getItem (int position) {//TODO auto-generated method Stubreturn Items.get (position);} @Overridepublic long getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic View GetView (int position, view contentview, viewgroup arg2) {//TODO auto-generated method Stubcontentvie W=m.get (position); if (contentview==null) {contentview=inflater.inflate (r.layout.listview_item, null); TextView text= (TextView) Contentview.findviewbyid (r.id.list_item_text); text.settext (items.get (position));} M.put(position, contentview); return contentview;} public void AddItem (String Item) {items.add (item); } }</pre><br> <pre name="code" class="html">public class Listviewactivity extends Activity implements Onscrolllistener {list<string> items = new Arraylist< ; String> (); Private ListView listview; private int visiblelastindex = 0; The last visual index of the private int visibleitemcount; The total number of visible items in the current form private Listviewadapter adapter; Private View loadmoreview; Private Button loadmorebutton; Private Handler Handler = new Handler (); @Override public void OnCreate (Bundle Savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.listview); Loadmoreview = Getlayoutinflater (). Inflate (r.layout.load_more, null); Loadmorebutton = (Button) Loadmoreview.findviewbyid (r.id.loadmorebutton); Loadmorebutton.setonclicklistener (new onclicklistener () {@Overridepublic void OnClick (View v) {//TODO auto-generated Method Stubloadmorebutton.settext ("loading in ..."); Set button text loading handler.postdelayed (new Runnable () { @Override public void Run () {loaddata (); Adapter.notifydatasetchanged (); After the data set changes, notify adapter listview.setselection (visiblelastindex-visibleitemcount + 1); Set the selected item Loadmorebutton.settext ("load many others"); Restore button text}}, 1000); }}); ListView = (listview) This.findviewbyid (r.id.listview1); Listview.addfooterview (loadmoreview); Set the bottom view of the List//listview.addheaderview (v)//settings list top view Initadapter (); Listview.setadapter (adapter); Take the initiative to set the adapter Listview.setonscrolllistener (this) for the listView of the ID is list; Added slide monitor Listview.setonitemclicklistener (new onitemclicklistener () {@Overridepublic void Onitemclick (adapterview& lt;? > arg0, View view,int position, long arg3) {//TODO auto-generated method Stubtoast.maketext (getapplicationcontExt (), items.get (position), toast.length_short). show ();}); }/** * Initialize adapter */private void Initadapter () {for (int i = 0; I <; i++) { Items.Add (string.valueof (i + 1)); } adapter = new Listviewadapter (items,this); }/** * is called when sliding */@Override public void onscroll (abslistview view, int firstvisibleitem, int VI sibleitemcount, int Totalitemcount) {this.visibleitemcount = visibleitemcount; Visiblelastindex = Firstvisibleitem + visibleItemCount-1; }/** * is called when sliding state changes */@Override public void onscrollstatechanged (abslistview view, int Scrollst Ate) {int itemslastindex = Adapter.getcount ()-1; The index of the last item of the dataset int lastIndex = Itemslastindex + 1; Plus the bottom of the Loadmoreview item if (scrollstate = = Onscrolllistener.scroll_state_idle && Visiblelastindex = = Lastind Ex) {//assuming that you are actively loading, you can place theStep load Data Code LOG.I ("loadmore", "loading ..."); }}/** * Simulate loading data */private void LoadData () {int count = Adapter.getcount (); for (int i = count; i < count + I++) {adapter.additem (string.valueof (i + 1)); } }}</pre><br>Finally look at the Effect:<p><p></p></p><p><p></p></p><p><p></p></p><p><p><br></p></p><p><p><br></p></p><p><p><br></p></p><br><p><p>ListView page Loading Data in Android</p></p></span>
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