• Android ListView带CheckBox实现单选


    第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

    <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:layout_weight="1"
            android:gravity="center_vertical"
            android:textcolor="@android:color/black">
    
            <checkbox
                android:id="@+id/main_list_item_checkBox"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginright="10dp"
                android:backgroundtintmode="multiply"
                android:clickable="false"
                android:focusable="false"
                android:focusableintouchmode="false"></checkbox>
        </textview>
    </linearlayout>
  • 相关阅读:
    @Order
    uri和url , 以及末尾加不加 '/'
    windows删除右键新建多余选项
    Typora 指南
    常见状态码/HttpStatusCode
    final 修饰符
    4. shiro-整合redis
    springboot 整合mybatis 一级缓存失效
    springboot查看具体访问的url片段
    JavaSE:NIO
  • 原文地址:https://www.cnblogs.com/zhujiabin/p/6432001.html
Copyright © 2020-2023  润新知