In the development of the app, you will often encounter the need to bulk cancel (delete) the data in the list. This requires ListView support batch selection, selection, radio and so on functions, do a more powerful ListView batch selection function is very necessary, then how to do?
It is conceivable that to support the bulk selection, that CheckBox is indispensable to use, the following, the use of ListView combined with the checkbox to achieve the volume of data selection.
First look at the effect of the picture, there is a picture of the truth:
First explain the Listview+checkbox support features that you will implement next:
- 1. External click "Edit" (Long press ListView of a certain item also can), appear check box;
- 2. Support Select, check, select All
- 3. Support for information to obtain selected data
Next, let's take a look at the implementation steps:
1. Define the List_item_data.xml, the contents of the list are displayed, the request contains a checkbox
<?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:background=" #ffffff "Andr oid:orientation= "Horizontal" > <linearlayout android:layout_width= "wrap_content" android:layout_height= "Wra" P_content "android:layout_gravity=" center_vertical "android:gravity=" center_vertical "android:orientation=" Horizo Ntal "> <checkbox android:id=" @+id/checkbox_operate_data "android:layout_width=" Wrap_content "Androi" d:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:layout_margin= "6DP" Android:vi
sibility= "Gone"/> <imageview android:id= "@+id/material_item_img" android:layout_width= "Wrap_content"
android:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:adjustviewbounds= "true" Android:scaletype= "Centercrop"Android:src=" @mipmap/ic_launcher "/> </LinearLayout> <linearlayout android:layout_width=" Fill_parent "android:layout_height=" wrap_content "android:layout_gravity=" center_vertical "android:orientation=" H Orizontal "android:paddingbottom=" 10DP "android:paddingtop=" 10DP "> <view android:id=" @+id/user_head_i MG "android:layout_width=" 5DP "android:layout_height=" "Fill_parent" android:background= "#4483c9"/> ; LinearLayout android:layout_width= "fill_parent" android:layout_height= wrap_content "android:orientation=" vert ical "> <textview android:id=" @+id/text_title "android:layout_width=" Wrap_content "android:layou
t_height= "Wrap_content" android:gravity= "center_vertical" android:singleline= "true" android:text= "title" Android:textcolor= "#555555" android:textsize= "16sp"/> <linearlayout android:layout_width= "Fill_pare NT "Android:layout_height= "Wrap_content" android:layout_margintop= "10DP" android:gravity= "center_vertical" Android:orientatio n= "Horizontal" > <textview android:id= "@+id/text_desc" android:layout_width= "Wrap_content" a ndroid:layout_height= "Wrap_content" android:layout_gravity= "center_vertical" android:gravity= "Bottom" an Droid:singleline= "true" android:text= "description Description Description Description Description description" android:textcolor= "#aaaaaa" android:textsize= "14SP"
/> </LinearLayout> </LinearLayout> </LinearLayout> </LinearLayout>
2. Define beans for data display
public class Databean {public
String ID;
public String title;
Public String desc;
public boolean Ischeck;
Public Databean (string ID, string title, String desc) {
this.id = ID;
this.title = title;
THIS.DESC = desc;
}
Note: The Databean contains the Ischeck property, which mainly flags whether the checkbox is selected.
3. Define the adapter of the data display, in which we need to implement two important functions: controlling whether the checkbox is displayed; Controlling whether the checkbox is displayed
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.TextView;
Import java.util.List;
public class Myadapter extends Baseadapter {private context mcontext;
Private list<databean> Mdatas;
Private Layoutinflater Minflater;
public Boolean flage = false;
Public Myadapter (Context Mcontext, list<databean> mdatas) {this.mcontext = Mcontext;
This.mdatas = Mdatas;
Minflater = Layoutinflater.from (This.mcontext);
@Override public int GetCount () {return mdatas.size ();
@Override public Object getitem (int i) {return mdatas.get (i);
@Override public long Getitemid (int i) {return i; @Override public View getview (int position, View Convertview, ViewGroup viewgroup) {Viewholder holder = null
; if (Convertview = = null) {
Dropdown Layout Convertview = minflater.inflate (r.layout.list_item_data, NULL);
Holder = new Viewholder ();
Holder.checkboxoperatedata = (CheckBox) Convertview.findviewbyid (r.id.checkbox_operate_data);
Holder.texttitle = (TextView) Convertview.findviewbyid (r.id.text_title);
Holder.textdesc = (TextView) Convertview.findviewbyid (R.ID.TEXT_DESC);
Convertview.settag (holder);
else {holder = (Viewholder) convertview.gettag ();
Final Databean Databean = mdatas.get (position);
if (Databean!= null) {Holder.textTitle.setText (databean.title);
Holder.textDesc.setText (DATABEAN.DESC);
Set the display status of the checkbox according to isselected if (flage) {holder.checkboxOperateData.setVisibility (view.visible);
else {holder.checkboxOperateData.setVisibility (view.gone);
} holder.checkboxOperateData.setChecked (Databean.ischeck); Note that this setting is not oncheckedchanglistener, or is worth thinking about the Holder.checkboxOperateData.setOnClickListener (new View.onclicklistener () {@Override public void OnClick (View v) {if (Databean.ischeck) {Databean.
Ischeck = false;
else {Databean.ischeck = true;
}
}
});
return convertview;
Class Viewholder {public CheckBox checkboxoperatedata;
Public TextView Texttitle;
Public TextView Textdesc;
}
}
Note the 1:flage field, used to flag whether a checkbox is displayed, and to control whether a checkbox is displayed in the GetView method by changing the value in the activity.
Note 2: Define the Setonclicklistener method of the checkbox, not the Oncheckedchanglistener, so that it is convenient to control whether the checkbox is selected.
4. Define Activity
public class Mainactivity extends activity {private button button;
Private ListView ListView;
Private list<databean> Mdatas;
Private Myadapter Madapter;
@Override protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);
Setcontentview (R.layout.activity_main);
Button = (button) Findviewbyid (R.id.button);
ListView = (ListView) Findviewbyid (R.id.listview);
Mdatas = new arraylist<> ();
for (int i = 0; i < i++) {Databean Databean = new Databean ("" + I, "evil", "Mountain without Edge, heaven and earth, but dare and June");
Mdatas.add (Databean);
} madapter = new Myadapter (this, mdatas);
Listview.setadapter (Madapter); /** * Edit, Cancel edit * @param view */public void btneditlist (view view) {Madapter.flage =!madapter.flage
;
if (madapter.flage) {Button.settext ("Cancel");
else {button.settext ("edit");
} madapter.notifydatasetchanged (); /** * All options * @param view/Public void Btnselectalllist (view view) {if (madapter.flage) {for (int i = 0; i < mdatas.size (); i++) {Mdatas.
Get (i). Ischeck = true;
} madapter.notifydatasetchanged (); }/** * @param view/public void btnnolist (view view) {if (madapter.flage) {for (int i = 0; I < mdatas.size ();
i++) {mdatas.get (i). Ischeck = false;
} madapter.notifydatasetchanged (); }/** * @param view/public void btnfanxuanlist (view view) {if (madapter.flage) {for (i) NT i = 0; I < mdatas.size ();
i++) {if (Mdatas.get (i). Ischeck) {mdatas.get (i). Ischeck = false;
else {mdatas.get (i). Ischeck = true;
} madapter.notifydatasetchanged (); /** * Get Selected Data * @param view/public void btnoperatelist (view view) {list<string> ids = n
EW arraylist<> ();
if (madapter.flage) {for (int i = 0; i < mdatas.size (); i++) { if (Mdatas.get (i). Ischeck) {Ids.add (Mdatas.get (i). ID);
} toast.maketext (Mainactivity.this,ids.tostring (), Toast.length_short). Show ();
LOG.E ("TAG", ids.tostring ());
}
}
}
And so on, the perfect batch choice of ListView will be realized, I hope you like.