Knowledge overview
ListView is one of the most common controls in Android, and when the ListView and CheckBox, Button, and other controls are used together, there are a number of problems, such as a ListView entry that is not selectable, a ListView memory Overflow, a select-all, and a reverse selection.
1.
Content using the ListView to create a menu
Create a Fruit class
Public class Fruit { PrivateString name;Private intimg Public Fruit(String name,intIMG) { This. name = name; This. img = img; } PublicStringGetName() {returnName } Public void SetName(String name) { This. name = name; } Public int getimg() {returnimg } Public void setimg(intIMG) { This. img = img; }}
Then create a Myfruitadapter
Public class myfruitadapter extends baseadapter{ PrivateList<fruit> Mdata;PrivateLayoutinflater Minflater;//Use an array to store the status of all checkboxes, checked to True, otherwise false Private Boolean[] mmanagerallcheckedbox; Public Myfruitadapter(list<fruit> mdata, Layoutinflater minflater) { This. Mdata = Mdata; This. Minflater = Minflater; Mmanagerallcheckedbox =New Boolean[Mdata.size ()]; }/** * for * @param selected checkbox */ Public void setitemstate(intPosition) {Mmanagerallcheckedbox[position]=!mmanagerallcheckedbox[position]; Notifydatasetchanged (); } Public void Itemcheckedall(){ for(inti =0; I < mdata.size (); i++) {mmanagerallcheckedbox[i]=true; } notifydatasetchanged (); } Public void Itemcheckedfan(){ for(inti =0; I < mdata.size (); i++) {Mmanagerallcheckedbox[i]=!mmanagerallcheckedbox[i]; } notifydatasetchanged (); }@Override Public int GetCount() {returnMdata.size (); }@Override PublicObjectGetItem(intPosition) {returnPosition }@Override Public Long Getitemid(intPosition) {returnPosition }@Override PublicViewGetView(Final intPosition, View Convertview, ViewGroup parent) {Viewholder VH =NULL;if(convertview==NULL) {Convertview = Minflater.inflate (R.layout.fruit_list_item,NULL); VH =NewViewholder (); Vh.checkbox = (CheckBox) Convertview.findviewbyid (R.id.item_fruit_checkbox); Vh.imageview = (ImageView) Convertview.findviewbyid (R.id.item_fruit_imageview); Vh.textview = (TextView) Convertview.findviewbyid (R.id.item_fruit_textview);//Store VH as a label in convertConvertview.settag (VH); }Else{//convertview not be empty when not re-findviewbyid, directly call the stored VH, improve efficiencyVH = (Viewholder) convertview.gettag (); } Fruit Fruit = Mdata.get (position); Vh.imageView.setImageResource (Fruit.getimg ()); Vh.textView.setText (Fruit.getname ()); Vh.checkBox.setOnCheckedChangeListener (NewCompoundbutton.oncheckedchangelistener () {@Override Public void oncheckedchanged(Compoundbutton Buttonview,BooleanisChecked) {mmanagerallcheckedbox[position]=ischecked; Notifydatasetchanged ();//Refresh DataLOG.D ("checkbox", position+"Status:"+ischecked); } }); Vh.checkBox.setChecked (Mmanagerallcheckedbox[position]);returnConvertview; }Private class viewholder{CheckBox checkbox; ImageView ImageView; TextView TextView; }}
Finally create a myfruitactivity
Public class myfruitactivity extends Activity { PrivateList<fruit> Mdata;PrivateLayoutinflater Minflater;PrivateListView Fruitlistview;PrivateMyfruitadapter Myfruitadapter;PrivateButton mheaderbtn;PrivateView Mheaderview;PrivateButton mfooterbtn;PrivateView Mfooterview;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_my_fruit); Fruitlistview = (ListView) Findviewbyid (R.id.fruit_list_view); Minflater = This. Getlayoutinflater (); InitData (); Myfruitadapter =NewMyfruitadapter (Mdata,minflater);//Load layout files for entry and end entries, respectivelyMheaderview = Minflater.inflate (R.layout.listview_header_layout,NULL); Mfooterview = Minflater.inflate (R.layout.listview_footer_layout,NULL);//listview calls its method of increasing the end and end entries to load the Kinsoku entryFruitlistview.addheaderview (Mheaderview); Fruitlistview.addfooterview (Mfooterview); MHEADERBTN = (Button) Findviewbyid (R.id.header_button); MFOOTERBTN = (Button) Findviewbyid (R.id.footer_button);//Set the listener for the end-to-end button respectively, and their functions are all select and reverseMheaderbtn.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {Myfruitadapter.itemcheckedall (); } }); Mfooterbtn.setonclicklistener (NewView.onclicklistener () {@Override Public void OnClick(View v) {Myfruitadapter.itemcheckedfan (); } }); Fruitlistview.setadapter (Myfruitadapter);//Set a click event for the ListView, and the corresponding checkbox will be selected whenever a view is selectedFruitlistview.setonitemclicklistener (NewAdapterview.onitemclicklistener () {@Override Public void Onitemclick(adapterview<?> parent, view view,intPositionLongID) {the reason for//position minus 1 is that header entries are added to all the ListView entries .Myfruitadapter.setitemstate (position-1); } }); }//Initialize the data to be loaded into the ListView Private void InitData() {Mdata =NewArraylist<> (); Fruit Apple =NewFruit ("Apple", r.mipmap.image); Fruit Pineapple =NewFruit ("Pineapple", r.mipmap.pk); Fruit pear =NewFruit ("Pear", R.MIPMAP.A_LOGO1); for(intI=0;i<4; i++) {mdata.add (Apple); Mdata.add (pineapple); Mdata.add (pear); } }}
Fruit_list_item as a layout for each entry
Where focusable is used to prevent the click events of the checkbox's parent control from being absorbed by the checkbox
<linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:orientation =" horizontal " android:layout_margin =" 15DP " android: Gravity = "center_vertical" android:layout_ Width = "match_parent" android:layout_heigh T = "match_parent" ; <checkbox android:id = "@+id/item_fruit_checkbox" android:layout_ Width = "wrap_content" android:layout_height< /span>= "wrap_content" android:focusable =< Span class= "Hljs-value" > "false" android:text = "Select fruit:" /> <ImageViewandroid:id="@+id/item_fruit_imageview"android:layout_width ="100DP"android:layout_height="100DP"/> <TextViewandroid:id="@+id/item_fruit_textview"android:layout_width ="Wrap_content"android:layout_height="wrap_content"/> </linearlayout>
Layout file
<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:paddingleft="@dimen/activity_horizontal_margin" Android:paddingright="@dimen/activity_horizontal_margin" Android:paddingtop="@dimen/activity_vertical_margin" Android:paddingbottom="@dimen/activity_vertical_margin" Tools:context="Com.example.administrator.mywidget.MyFruitActivity"> <ListViewandroid:id="@+id/fruit_list_view"android:layout_width= "Match_parent" android:layout_height="Match_parent"> </ListView></relativelayout>
Create a listview_header_layout as the first entry layout for the ListView
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns:android="Http://schemas.android.com/apk/res/android" Android:orientation="vertical"android:layout_width="Match_parent" Android:layout_height="Match_parent"> <buttonandroid:id= "@+id/header_button"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Select All "/ > </linearlayout>
Create a trailing entry layout with a ListView
<?xml version= "1.0" encoding= "Utf-8"?><linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android" android:orientation =" vertical " android:layout_width =" match_parent " and Roid:layout_height =; <buttonandroid:id= "@+id/footer_button"android:layout_width=" Wrap_content "android:layout_height=" Wrap_content "android:text=" Reverse selection "/ > </linearlayout>
Operation Result:
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
Important controls for Android control many problems with the ListView solution