The ListView uses Viewholder to store controls, avoiding multiple creation of search space resources and the use of Popupwindow.

Source: Internet
Author: User

1, the ListView write Adapater time in the GetView, with the local definition of the view loaded XML after calling Findviewbyid (), When calling Findviewbyid in OnCreate (), the activity often does not load the ListView to load the Item.xml so found is actually null, resulting in an error.

2, Popupwindow for class members, in the OnCreate can only define an "empty" Popuwindow, that is, with the new Popupwindow (), tested in the OnCreate () method to set the Popupwindow property is not valid. and OnCreate () inside can not display Popupwindow, otherwise error.

3. In a ListView item, there is a button, a checkbox, or oneself in the GetView () Overwrite method defines a view of the item in the listener event, if again Setonitemclicklistener, It is likely to result in a partial view item grab focus, while the item click event Fails, and the demo below is invalidated on the real machine and available on the simulator. After the change to all listen to the local view.

4, the ListView GetView time each time must create the item time has three kinds of methods, 1 directly creates. 2 The view is deposited into the Convertview parameter, 3, in order to improve the efficiency can be defined viewholder to save the view in the inside, and then call Settag (), the next time you create the item directly with the Gettag () to get the view, set the value, to improve efficiency See Code for specific methods.

See Code below:

Main_xml:

<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 "Android:paddi ngbottom= "@dimen/activity_vertical_margin" android:paddingleft= "@dimen/activity_horizontal_margin" Android: paddingright= "@dimen/activity_horizontal_margin" android:paddingtop= "@dimen/activity_vertical_margin" tools: context= "Com.example.deomhouxuan.mainactivity$placeholderfragment" > <relativelayout android:layout_width= "200DP" android:layout_height= "wrap_content" android:id= "@+id/qq" > <edittext Android         : id= "@+id/ed_t" android:layout_width= "200DP" android:layout_height= "wrap_content"/> <imagebutton Android:layout_width= "Wrap_content" android:layout_height= "Wrap_content" Android:layout_alignparentri   Ght= "true" android:background= "@drawable/down_arrow"     android:onclick= "Imbonclick" android:id= "@+id/imb"/></relativelayout></relativelayout> 

Item_xml

<?xml version= "1.0" encoding= "Utf-8"? ><relativelayout xmlns:android= "http://schemas.android.com/apk/res/ Android "Android:layout_width=" 200DP "android:layout_height=" Match_parent "> <imageview Android : contentdescription= "@string/app_name" android:layout_width= "wrap_content" android:layout_height= "Wrap_conte      NT "android:src=" @drawable/user "android:id=" @+id/ima_v "android:layout_margin=" 5DP "/><textview Android:layout_width= "Wrap_content" android:layout_height= "wrap_content" android:id= "@+id/text_v" android:layout _centerhorizontal= "true" android:layout_centervertical= "true"/><imageview android:contentdescription= "@ String/app_name "android:layout_width=" wrap_content "android:layout_height=" wrap_content "android:id=" @+id/delete "Android:background=" @drawable/delete "android:layout_centervertical=" true "android:layout_alignparentright=" True "/></relativelayout>

Activity

Package Com.example.deomhouxuan;import Java.util.arraylist;import Android.app.activity;import Android.graphics.drawable.bitmapdrawable;import Android.os.bundle;import Android.view.view;import Android.view.view.onclicklistener;import Android.view.viewgroup;import Android.widget.baseadapter;import Android.widget.edittext;import Android.widget.imageview;import Android.widget.listview;import Android.widget.popupwindow;import Android.widget.textview;public class Mainactivity extends Activity {private EditText ed;private arraylist<string> data;public ListView list;public Popupwindow popu;    Mylistadapter adapter;        @Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        Setcontentview (R.layout.activity_main);                    Initview ();    } public void Initview () {ed= (EditText) Findviewbyid (r.id.ed_t);    Data=new arraylist<string> ();    for (int x=0;x<10;x++) Data.add (string.valueof (1234123+x));    List=new ListView (this);    Popu=new Popupwindow ();    Adapter=new Mylistadapter ();    List.setadapter (adapter);//System.out.println (Popu==null); } public void Imbonclick (View v) {///experimentally found: Popupwindow except for an empty object in the main line range new, all other property settings are invalid Popu.setwidth (ED.GETW     Idth ());     Popu.setheight (200);     Set the line between the ListView item to 0; List.setdividerheight (0);   Popu.setcontentview (list);   if (popu.isshowing ()) {Popu.dismiss ();   } else Popu.showasdropdown (ed);    The following 2 sentence is to let the popup behind also get the focus, click Back when the popup hidden Popu.setbackgrounddrawable (New bitmapdrawable ());        Popu.setfocusable (TRUE); When setting a click event here, because the delete in ListItem and the item Scramble corner causes item to be able on the emulator, the real machine fails,//So in order to avoid, the listener event is defined in GetView//List.setfocusable (True );//List.setonitemclicklistener (new Onitemclicklistener () {//@Override//public void Onitemclick (adapterview<? > Parent, View view,//int position, long id) {//ed.settext (Data.get (position));////system.out.println (Popu==null)       ;//}//}); } class MylistadaptER extends baseadapter {@Overridepublic int getcount () {return data.size ();} @Overridepublic Object getItem (int position) {return null;} @Overridepublic long Getitemid (int position) {//TODO auto-generated method Stubreturn position;} @Overridepublic View GetView (final int position, view Convertview, ViewGroup parent) {//slowest method, create view each time, and return//view v= Getlayoutinflater (). Inflate (R.layout.item, null),//textview tv= (TextView) V.findviewbyid (R.ID.TEXT_V);// Tv.settext (Data.get (position))//return v;////general method, the view is deposited into Convertview, the next direct take//if (Convertview==null)//{// Convertview=getlayoutinflater (). Inflate (R.layout.item, null);//textview tv= (TextView) Convertview.findviewbyid ( R.ID.TEXT_V);//tv.settext (Data.get (position));//}//else//{//((TextView) Convertview.findviewbyid (r.id.text_v)). SetText (Data.get (position))//}//return convertview;//fastest method Viewholder viewholder;if (convertview==null) { Convertview=getlayoutinflater (). Inflate (R.layout.item, NULL); TextView tv= (TextView) Convertview.findviewbyid (r.id.text_v); viewholder=new Viewholder (); viewholder.tv=tv;viewholder.delete= (ImageView) Convertview.findviewbyid ( R.id.delete); Convertview.settag (Viewholder);} Else{viewholder= (Viewholder) Convertview.gettag ();} Viewholder.tv.setText (Data.get (position));//Add Click to delete Event Viewholder.delete.setOnClickListener (new Onclicklistener () {@Overridepublic void OnClick (View v) {data.remove (position); adapter.notifydatasetchanged ();}}); /Add event Viewholder.tv.setOnClickListener for click Data (New Onclicklistener () {@Overridepublic void OnClick (View v) {Ed.settext (Data.get (position));p Opu.dismiss ();});   return Convertview;} }//Static Vieholder class Storage View object static class Viewholder {TextView TV;  ImageView Delete; }   }

  

The ListView uses Viewholder to store controls, avoiding multiple creation of search space resources and the use of Popupwindow.

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.