解决listView嵌套CheckBox的选中错乱

先来看看效果图:

解决listView嵌套CheckBox的选中错乱_第1张图片      解决listView嵌套CheckBox的选中错乱_第2张图片

因为是个演示的Demo所以就写假数据了  -_ -~!


这样你滑动的时候就不会因为复用问题,导致checkBox选中错乱

1.布局文件

2. item的布局文件



 

 .
    
3.设置Main方法

public class MainActivity extends AppCompatActivity implements AdapterView.OnItemClickListener {

    private ListView mListView;
    private CheckListAdapter adapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        initView();
    }

    private void initView() {
        mListView = (ListView) findViewById(R.id.check_list);
        adapter = new CheckListAdapter(this);
        mListView.setAdapter(adapter);
        mListView.setOnItemClickListener(this);
    }

    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        //点击listItem设置checkBox的状态,未选中变为选中
        adapter.getItem(position).setSelected(!adapter.getItem(position).isSelected());
        adapter.notifyDataSetChanged();
    }
}
4.设置listView的Adapter
package com.example.checklistview.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;

import com.example.checklistview.R;
import com.example.checklistview.model.Model;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ALong on 2017/4/8.
 */

public class CheckListAdapter extends BaseAdapter implements CompoundButton.OnCheckedChangeListener {

    List data;
    LayoutInflater inflater;

    public CheckListAdapter(Context context) {

        this.inflater=LayoutInflater.from(context);
        this.data=new ArrayList<>();
        for (int i = 0; i < 30; i++) {
            Model model=new Model();
            model.setName("今天是个好日子"+i);
            data.add(model);
        }
    }

    @Override
    public int getCount() {
        return data!=null?data.size():0;
    }

    @Override
    public Model getItem(int position) {
        return data.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder holder;
        if (convertView == null) {
           convertView=inflater.inflate(R.layout.list_item,parent,false);
            holder=new ViewHolder(convertView);
            convertView.setTag(holder);
        }else {
            holder= (ViewHolder) convertView.getTag();
        }
        //一定要在绑定数据之前进行设置标记位
        holder.checkBox.setTag(position);

        holder.textView.setText(getItem(position).getName());
        holder.checkBox.setChecked(getItem(position).isSelected());
        //标记一个Tag记录当前checkbox的位置

        holder.checkBox.setOnCheckedChangeListener(this);

        return convertView;
    }

    @Override
    public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
        Integer position=(Integer)buttonView.getTag();
        getItem(position).setSelected(isChecked);
    }

    public static class ViewHolder{
        TextView textView;
        CheckBox checkBox;
        public ViewHolder(View itemView) {
            textView= (TextView) itemView.findViewById(R.id.item_text);
            checkBox= (CheckBox) itemView.findViewById(R.id.item_check);
        }
    }
}
5.用到的模型类,属性(1.name 2.是否选中)

public class Model {

    private String name;

    private boolean isSelected;
//生成set get 方法
    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public boolean isSelected() {
        return isSelected;
    }

    public void setSelected(boolean selected) {
        isSelected = selected;
    }
}
好了,完美的解决了,希望可以帮助到你!


每天比别人多学0.01,你会知道的更多


你可能感兴趣的:(Android)