The values of hoiceMode are: 1.none; 2.singleChoice; 3multipleChoice
As the name suggests: 1 is nothing, 2 is single selection mode, 3 is multiple selection mode
So what is the effect of the listview after selecting the corresponding value? Let's take a look at them separately.
Let me talk about the results first, and then give an example later.
When 2 is selected, listview.getCheckedItemPosition() is valid. What does it mean? When 1 and 3 are selected, this aspect returns -1; when 2 is selected, this method returns the subscript of the selected item.
When 3 is selected, list.getCheckedItemPositions() is valid. At this time, a SparseBooleanArray object is returned, which is actually a Map<Object, boolean>. When used, for example, sparseBooleanArray.get(0) can return the first item boolean value, whether selected
Single-select mode example:
Core fragments in main.xml
<ListView
android :id = "@+id/list"
android :layout_width = "fill_parent"
android :layout_height = "fill_parent"
android :choiceMode = "singleChoice" >
</ListView >
Core fragment in MainActivity
// GENRES是个String数组
list .setAdapter ( new ArrayAdapter < String>( this ,
android . R. layout .simple_list_item_single_choice , GENRES ));
list .setItemsCanFocus ( false );
list .setChoiceMode ( ListView .CHOICE_MODE_SINGLE );
operation result
Or another way of writing:
MainActivity里的核心片段
listView = ( ListView ) findViewById (R . id. list );
MyAdapter adapter = new MyAdapter ( this);
listView .setAdapter ( adapter );
MyAdapter code snippet:
public class MyAdapter extends BaseAdapter {
String [] arrays = { "1" , "2" , "3" };
Context context ;
public MyAdapter ( Context context ) {
this .context = context ;
}
@Override
public int getCount () {
return arrays .length ;
}
@Override
public Object getItem ( int position ) {
return arrays [position ];
}
@Override
public long getItemId ( int position ) {
return position ;
}
@Override
public View getView ( int position , View convertView , ViewGroup parent) {
if ( convertView == null ) {
//写法1
CountryView countryView = new CountryView(context);
countryView.setTitle(arrays[position]);
convertView = (View)countryView;
//写法2
// CheckedTextView checkedTextView = new CheckedTextView( context );
// checkedTextView . setText (arrays [position ]);
// checkedTextView. setCheckMarkDrawable (R . drawable .checkbox_bg );
// convertView = ( View )checkedTextView ;
//写法3
// View view = LayoutInflater.from(context).inflate(android.R.layout.simple_list_item_single_choice, null);
// convertView = (View)view;
}
return convertView ;
}
}
The checkbox_bg file in the drawable directory :
< selector xmlns :android =" http://schemas.android.com/apk/res/android" >
<item android : drawable ="@android:drawable/checkbox_on_background"
android : state_checked ="true" ></ item>
<item android : drawable ="@android:drawable/checkbox_off_background"
android : state_checked ="false" ></ item>
</ selector >
Code snippet of CountryView:
public class CountryView extends LinearLayout implements Checkable {
private TextView textView ;
private CheckBox checkBox ;
public CountryView ( Context context , AttributeSet attrs ) {
super (context , attrs);
initView ( context );
}
public CountryView ( Context context , AttributeSet attrs , int defStyle) {
super (context , attrs, defStyle );
initView ( context );
}
public CountryView ( Context context ) {
super (context );
initView ( context );
}
private void initView ( Context context ) {
View view = LayoutInflater. from( context ).inflate ( R. layout .country_view , this );
textView = ( TextView ) view . findViewById (R .id . list_text );
checkBox = ( CheckBox ) view . findViewById (R .id . list_checkbox );
}
public void setTitle ( String title ) {
textView . setText (title );
}
@Override
public void setChecked ( boolean checked ) {
checkBox . setChecked (checked );
}
@Override
public boolean isChecked () {
return checkBox .isChecked ();
}
@Override
public void toggle () {
checkBox . toggle();
}
}
country_view layout file:
<? 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 :gravity = "center_vertical"
android :orientation = "horizontal" >
<TextView
android : id= "@+id/list_text"
android : layout_width ="wrap_content"
android : layout_height ="wrap_content"
android : layout_weight ="1" />
<CheckBox
android : id= "@+id/list_checkbox"
android : layout_width ="wrap_content"
android : layout_height ="wrap_content"
android : layout_weight ="0"
android : clickable ="false"
android : focusable ="false"
android : focusableInTouchMode ="false" />
</ LinearLayout >
Screenshot corresponding to writing method 1:
Screenshot corresponding to writing 2:
Example of multiple selection mode:
Core fragments in main.xml
< ListView
android :id = "@+id/list"
android :layout_width = "fill_parent"
android :layout_height = "fill_parent"
android :choiceMode = "multipleChoice" >
</ ListView >
Other codes are the same as the single-select module example