关于android listview属性choiceMode的思考

hoiceMode的值有:1.none;2.singleChoice;3multipleChoice

顾名思义:1是什么都没有,2是单选模式,3是多选模式

那到底选了相应的值后listview有什么效果呢?下面来分别看看。

先说下结果,后面再举个例子。

        当选择2的时候,listview.getCheckedItemPosition()有效,什么意思呢,选1和3的时候,这个方面返回-1;而选2的时候,这个方法返回选中的那个item的下标。

        当选择3的时候,list.getCheckedItemPositions()有效,此时返回一个SparseBooleanArray对象,实际上是一个Map<Object, boolean>的东西, 用的时候比如sparseBooleanArray.get(0)就可以返回第一项的boolean值,是否选中

单选模式例子:

main.xml里的核心片段

<ListView
    android :id = "@+id/list"
    android :layout_width = "fill_parent"
    android :layout_height = "fill_parent"
    android :choiceMode = "singleChoice" >
</ListView >
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 );

运行结果

或者另外的写法:

MainActivity里的核心片段
listView  = ( ListView ) findViewById (R . id. list );
MyAdapter adapter = new MyAdapter ( this);
listView .setAdapter ( adapter );

MyAdapter  代码片段:

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 ;
    }
}

drawable目录下  checkbox_bg文件:
 

< 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 >
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布局文件:

<? 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 >


 

写法1对应的截图:

写法2对应的截图:

多选模式例子:

main.xml里的核心片段

< ListView
    android :id = "@+id/list"
    android :layout_width = "fill_parent"
    android :layout_height = "fill_parent"
    android :choiceMode = "multipleChoice" >
</ ListView >

其他代码同单选模块例子

猜你喜欢

转载自blog.csdn.net/az44yao/article/details/112687621