Android ListView带CheckBox实现单选

这个应该是个经常会遇到的需求,网上感觉说的有点太长于是自己抽空干脆记录一下。

用bean记录状态,每次getView的时候去判断这个bean的状态

效果图:

首先是bean:

public class Bean {
    private boolean isChecked;
    private String msg = "这是一条测试数据";

    public Bean(){

    }
    public Bean(boolean isCheched) {
        this.isChecked = isCheched;
    }
    public boolean isChecked() {
        return isChecked;
    }
    public void setChecked(boolean isChecked) {
        this.isChecked = isChecked;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    } 
}

然后在getView的时候判断状态

public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder mViewHolder;
        if(convertView == null){
            convertView = mInfater.inflate(R.layout.list_item, null);
            mViewHolder = new ViewHolder(convertView);
            convertView.setTag(mViewHolder);
        }else{
            mViewHolder = (ViewHolder) convertView.getTag();
        }
        mViewHolder.mTextView.setText(mDatas.get(position).getMsg());
        if(mDatas.get(position).isChecked()){//状态选中
            mViewHolder.mCheckBox.setChecked(true);
        }else{
            mViewHolder.mCheckBox.setChecked(false);
        }
        return convertView;
    }

这样我们就能通过bean的状态来控制checkBox的选中与否了,但是onItemCLick的时候的操作才是关键

java代码

public class MainActivity extends ActionBarActivity {
    private ListView mListView;
    private ListAdapter mAdapter;
    private List mDatas;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.main_listView);
        mDatas = new ArrayList();
        for (int i = 0; i < 20; i++) {
            if(i == 0){//默认选中第一个
                mDatas.add(new Bean(true));
                continue;
            }
            mDatas.add(new Bean(false));
        }
        mAdapter = new ListAdapter(this, mDatas);
        mListView.setAdapter(mAdapter);
        mListView.setOnItemClickListener(mOnItemClickListener);
    }

    private final OnItemClickListener mOnItemClickListener = new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView adapterView, View view, int position, long arg3) {
            for (Bean bean : mDatas) {//全部设为未选中
                bean.setChecked(false);
            }
            mDatas.get(position).setChecked(true);//点击的设为选中
            mAdapter.notifyDataSetChanged();

        }
    };
}

XML


<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >

    <TextView 
        android:id="@+id/main_list_item_text"
        android:layout_width="0dp"
        android:layout_height="match_parent"
        android:textColor="@android:color/black"
        android:layout_weight="1"
        android:gravity="center_vertical"
        />
    
    <CheckBox 
        android:id="@+id/main_list_item_checkBox"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_vertical"
        android:backgroundTintMode="multiply"
        android:layout_marginRight="10dp"  
        android:clickable="false"
        android:focusableInTouchMode="false"
        android:focusable="false"
        />
LinearLayout>

于是这样ListView的CheckBox就能实现单选了,RecyclerView同理。
以上仅是个人用法,当然也有其他办法实现,看个人的喜好吧

你可能感兴趣的:(Android)