码迷,mamicode.com
首页 > 移动开发 > 详细

Android ListView带CheckBox实现单选

时间:2016-05-12 13:04:10      阅读:472      评论:0      收藏:0      [点我收藏+]

标签:

这个应该是个经常会遇到的需求,网上找了很多的demo感觉说的有点太长于是自己抽空干脆记录一下,目前我实现listView带CheckBox单选主要用两种方法

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

2,判断点击的position是否跟getView的position一致,这种办法耦合性较低不建议使用


效果图:技术分享


第1种方法:

首先是我们的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的时候的操作才是关键

public class MainActivity extends ActionBarActivity {
    private ListView mListView;
    private ListAdapter mAdapter;
    private List<Bean> 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<Bean>();
        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

<?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: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获取事件的优先级大于listView故需设置focusable = false -->
    <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就能实现单选了


第二种:

这种办法不推荐使用

public class MainActivity extends ActionBarActivity {
    private ListView mListView;
    private ListAdapter mAdapter;
    private List<Bean> mDatas;
    public static int pos;//记录选中的position
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mListView = (ListView) findViewById(R.id.main_listView);
        mDatas = new ArrayList<Bean>();
        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) {
            pos = position;
            mAdapter.notifyDataSetChanged();
        }
    };

}

getView的时候判断MainActivity的pos是否等于getView的position

    @Override
    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(MainActivity.pos == position){//MainActivity的pos是否等于position
            mViewHolder.mCheckBox.setChecked(true);
        }else{
            mViewHolder.mCheckBox.setChecked(false);
        }
        return convertView;
    }

不推荐使用第二种办法,这种办法会让程序的耦合性变得很低,并且全局变量始终会有一个Class 对象的句柄指向它除非这个Class对象要被回收了否则静态变量是不会被GC掉

以上仅是个人用法,当然也有其他办法实现,看个人的喜好吧

Android ListView带CheckBox实现单选

标签:

原文地址:http://blog.csdn.net/xuyonghong1122/article/details/51362674

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!