Android ListView Adapter and a variety of monitoring, efficiency improvements

Source: Internet
Author: User

Before writing a blog about the ListView, now feel that the blog about the ListView is not fully understood. But some methods are preferable, such as flexible listening, writing adapters. Link here Android ListView Long Press, click on various event snaps. The one-click Listener is an item setting for monitoring, which is not efficient.

The process of continuous work is also an ongoing process of summarizing. Now we have a more thorough understanding of the ListView, so I have re-written a demo of practice. This demo has data sources, layouts, adapters, and various monitoring and efficiency improvements related to the ListView. Now for these understandings, you want to write a generic adapter, but the discovery is still a bit difficult, is the data source problem, and layout. But it is also possible to write a generic adapter, using the inheritance method. The data source for the ListView and the implementation of the layout implementation are called by the function, and then the inheritance is overridden by these methods. Because I am in the company project is like this, the effect is good. Here's a look at the ListView.

First, the entity class Itemtest

This is a custom ListView, so write an entity class as an adaptation type for the ListView adapter.

Package com.example.customlistviewdemo;/** * Entity class Itemtest * @author mmsx * Blogger Blog URL: http://blog.csdn.net/qq_16064871 */pub Lic class Itemtest {private int mimageviewid;private String mstrname;public itemtest () {}public itemtest (int imageviewid, String strName) {this.mimageviewid = Imageviewid;this.mstrname = StrName;} public int Getimageviewid () {return mimageviewid;} Public String Getstrname () {return mstrname;}}
Second, the ListView for the entity class Itemtest adapter Selfadapt

The ListView adapter inherits from Baseadapter, passing through the Selfadapt's constructor to the activity's life cycle, the ListView layout, and the data source.

In it used to determine whether Convertview is empty, to reduce the loading layout, improve efficiency.

It also has an internal class viewholder to reduce the ID of the lookup layout, caches the Convertview.settag (Viewholder) with a label, and then takes out Viewholder = (Viewholder) Convertview.gettag ();

In this way, the efficiency of the ListView has a very good effect.

Package Com.example.customlistviewdemo;import Java.util.arraylist;import Android.content.context;import Android.view.layoutinflater;import Android.view.view;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.checkbox;import Android.widget.compoundbutton;import Android.widget.toast;import Android.widget.compoundbutton.oncheckedchangelistener;import Android.widget.imageview;import android.widget.textview;/** * ListView Adapter inherits from Baseadapter * @author mmsx * Blogger Blog URL: http:/ /blog.csdn.net/qq_16064871 */public class Selfadapt extends Baseadapter implements Oncheckedchangelistener{private Context mcontext;private int mresourcelayoutid;private layoutinflater mlayoutinflater;private ArrayList<ItemTest  > mlist;/** * Adapter Constructor * @param context * @param resourcelayoutid * @param list */public selfadapt (context context, int Resourcelayoutid,arraylist<itemtest> list) {This.mcontext = Context;this.mresourcelayoutid = ResourceLayoutID; This.mlist = list;//Get xmland Instantiate mlayoutinflater = (layoutinflater) mcontext.getsystemservice (context.layout_inflater_service);} @Overridepublic int GetCount () {return mlist! = null? Mlist.size (): 0;} @Overridepublic Object getItem (int position) {return position;} @Overridepublic long Getitemid (int position) {return position;} @Overridepublic view GetView (int position, view Convertview, ViewGroup parent) {Viewholder Viewholder = null;if (convertvi EW = = NULL) {//This write does not have to load the layout every time, and find the ID, improve the efficiency of the ListView Convertview = mlayoutinflater.inflate (Mresourcelayoutid, NULL); Viewholder = new Viewholder (); Viewholder.imageview = (ImageView) Convertview.findviewbyid (R.ID.IMAGEVIEW1); Viewholder.textview = (TextView) Convertview.findviewbyid (r.id.textviewtest); viewholder.checkbox = (checkbox) Convertview.findviewbyid (r.id.checkbox1);//Tag Join Tagconvertview.settag (Viewholder);}   else {//when Convertview is not empty, remove Viewholder = (viewholder) Convertview.gettag () from the label Viewholder; }itemtest itemtest = new Itemtest (); itemtest = Mlist.get (position); viewhoLder.imageView.setImageResource (Itemtest.getimageviewid ()); ViewHolder.textView.setText (Itemtest.getstrname ()) ;//Set the monitor ViewHolder.checkbox.setOnCheckedChangeListener (this) for the checkbox; ViewHolder.checkbox.setTag (position); return Convertview;} Inner class implementation, lifting ListView efficiency class Viewholder {public ImageView imageview;public TextView textview;public checkbox checkbox; @Overridepublic void OnCheckedChanged (Compoundbutton buttonview, Boolean isChecked) {int nposition = ((Integer) Buttonview.gettag ()). Intvalue (); Toast.maketext (Mcontext, string.valueof (nposition) + "checkbox's Click" + string.valueof (isChecked), Toast.length_short). Show ();}}
Third, mainactivity

In this case, a ListView click is set, and the long-pressed interface listens.

Listview.setonitemclicklistener (this);    Set Click Listen, interface implementation Listview.setonitemlongclicklistener (this);  Set long press listening, interface implementation

Set both of these to be interface

Implements Onitemclicklistener,onitemlongclicklistener
See all the code below

Package Com.example.customlistviewdemo;import Java.util.arraylist;import Android.os.bundle;import Android.app.activity;import Android.view.view;import Android.widget.adapterview;import Android.widget.adapterview.onitemlongclicklistener;import Android.widget.listview;import Android.widget.adapterview.onitemclicklistener;import android.widget.toast;/** * ListView comparison Standard notation and improved listview efficiency * @ Author Mmsx * Blogger Blog URL: http://blog.csdn.net/qq_16064871 */public class Mainactivity extends Activity implements Onitemclic Klistener,onitemlongclicklistener{private arraylist<itemtest> marraylist = new ArrayList<ItemTest> (); overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview ( R.layout.activity_main); Initui ();} private void Initui () {if (marraylist! = null) {marraylist.clear (); for (int i = 0; i <; i++) {Itemtest ItemTest1 = n EW Itemtest (R.DRAWABLE.BMP1, "picture One"); Marraylist.add (itemTest1); Itemtest itemTest2 = new Itemtest (R.DRAWABLE.BMP2, "picture Two"); Marraylist.add (ITEMTEST2);}} ListView ListView = (ListView) Findviewbyid (R.id.listview1); Selfadapt selfadapt = new Selfadapt (Getapplicationcontext (), R.layout.listview_item, marraylist); listView.setAdapter    (SELFADAPT); Listview.setonitemclicklistener (this);  Set Click Listen, interface implementation Listview.setonitemlongclicklistener (this); Set long press listen, interface implementation} @Overridepublic void Onitemclick (adapterview<?> parent, view view, int Position,long ID) { Toast.maketext (This, "ListView click" + parent.getitematposition (position), Toast.length_short). Show (); @Overridepublic Boolean Onitemlongclick (adapterview<?> parent, View view,int position, long id) {Toast.maketext ( This, "Long press of ListView", Toast.length_short). Show (); return true;}}
Four, the layout of the ListView Listview_item

is to implement the entity class Itemtest, I have added chexbox control monitoring, example implementation.

Where this sentence is sometimes very important android:descendantfocusability= "blocksdescendants"

If your custom ListViewItem has a button or checkable child control, then the default focus is given to the child control. So I need to add this sentence.

<?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=" 50DP "android:des Cendantfocusability= "blocksdescendants" android:orientation= "horizontal" > <imageview android:id= "@+id/ ImageView1 "android:layout_width=" 50DP "android:layout_height=" 50DP "android:src=" @drawable/bmp1 "/&G    T        <textview android:id= "@+id/textviewtest" android:layout_width= "0DP" android:layout_height= "50DP" android:layout_weight= "1" android:gravity= "center" android:text= "TextView" android:textcolor= "# ff236a9c "/> <checkbox android:id=" @+id/checkbox1 "android:layout_width=" Wrap_content "Andro id:layout_height= "Wrap_content" android:textcolor= "#FF236A9C" android:text= "select"/></linearlayout>< /pre>
Wu, Activity_main

<relativelayout xmlns:android= "http://schemas.android.com/apk/res/android"    xmlns:tools= "http// Schemas.android.com/tools "    android:layout_width=" match_parent "    android:layout_height=" Match_parent "    tools:context= ". Mainactivity ">    <listview        android:id=" @+id/listview1 "        android:layout_width=" Match_parent "        android:layout_height= "wrap_content"        android:layout_alignparenttop= "true"        android:layout_ Centerhorizontal= "true" >    </ListView></RelativeLayout>
vi. Realization of



In addition I also collect various small knowledge of the ListView, such as Divider line, click Do not default effect, ListView does not scroll with scroll bar, etc., later will write a blog alone.
Project Resources Download:
Reprint please indicate the source of the blog site: http://blog.csdn.net/qq_16064871
If there is a reprint does not indicate the source of the blog URL, or did not get the author's consent. The author holds copyright ownership.

Android ListView Adapter and a variety of monitoring, efficiency improvements

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.