Listview嵌套CheckBOX 实现全选反选取消,并对选中项进行标记

项目中遇到类似需求 ,参考前辈代码自己进行整改。直接发码。

Activity中

public class ScreenActivity extends AppCompatActivity implements View.OnClickListener {
    private static final String TAG = ScreenActivity.class.getSimpleName();
    private SharedPreferences sp;
    private SharedPreferences.Editor editor;
    private Button but_editor;
    private Button but_all;
    private Button but_sure;
    private Button but_invert;
    private Button but_clean;
    private ListView listview;
    private List<Experiment.DataBean.PlotsBean.PhenotypesBean> phenotypesBeanList;
    private ScreenAdapter adapter;
    private TextView Sure;

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

    private void initView() {
        sp = this.getSharedPreferences("person", MODE_PRIVATE);// 初始化 SharedPreferences 储存
        editor = sp.edit();//SharedPreferences 储存 可编辑化

        but_editor = ((Button) findViewById(R.id.bt_editor));
        but_editor.setOnClickListener(this);
        but_all = ((Button) findViewById(R.id.bt_all));
        but_all.setOnClickListener(this);
        but_invert = ((Button) findViewById(R.id.bt_invert));
        but_invert.setOnClickListener(this);
        but_clean = ((Button) findViewById(R.id.bt_clean));
        but_clean.setOnClickListener(this);
        Sure = ((TextView) findViewById(R.id.OK));
        Sure.setOnClickListener(this);

        listview = ((ListView) findViewById(R.id.listView));
        Gson gson = new Gson();
        Experiment experiment = gson.fromJson(sp.getString("TYPE", ""), Experiment.class);
        phenotypesBeanList = experiment.getData().get(0).getPlots().get(0).getPhenotypes();
        Log.e(TAG, "-----------" + phenotypesBeanList);
        adapter = new ScreenAdapter(this, phenotypesBeanList);
        listview.setAdapter(adapter);
        listview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            private CheckBox cb;

            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                final Experiment.DataBean.PlotsBean.PhenotypesBean phenotypesBean = phenotypesBeanList.get(position);
                cb = ((CheckBox) view.findViewById(R.id.screen_checkbox));
                cb.toggle();
                if (phenotypesBean.isCheck) {
                    phenotypesBean.isCheck = false;
                } else {
                    phenotypesBean.isCheck = true;
                }
            }
        });
    }

    @Override
    protected void onResume() {
        if (getRequestedOrientation() != ActivityInfo.SCREEN_ORIENTATION_PORTRAIT) {  //设置为竖屏
            setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
        }
        super.onResume();
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.bt_editor:
                adapter.flage = !adapter.flage;
                if (adapter.flage) {
                    but_editor.setText("取消");
                } else {
                    but_editor.setText("编辑");
                }
                adapter.notifyDataSetChanged();
                break;
            case R.id.bt_all:
                if (adapter.flage) {
                    for (int i = 0; i < phenotypesBeanList.size(); i++) {
                        phenotypesBeanList.get(i).isCheck = true;
                    }
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.bt_invert:
                if (adapter.flage) {
                    for (int i = 0; i < phenotypesBeanList.size(); i++) {
                        if (phenotypesBeanList.get(i).isCheck) {
                            phenotypesBeanList.get(i).isCheck = false;
                        } else {
                            phenotypesBeanList.get(i).isCheck = true;
                        }
                    }
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.bt_clean:
                if (adapter.flage) {
                    for (int i = 0; i < phenotypesBeanList.size(); i++) {
                        phenotypesBeanList.get(i).isCheck = false;
                    }
                    adapter.notifyDataSetChanged();
                }
                break;
            case R.id.OK:
                List<String> ids = new ArrayList<>();
                if (adapter.flage) {
                    for (int i = 0; i < phenotypesBeanList.size(); i++) {
                        if (phenotypesBeanList.get(i).isCheck) {
                            ids.add(String.valueOf(i));
                        }
                    }
                    Log.e(TAG, "选中项是------------>" + ids.toString());
                }
                break;

        }
    }
}

适配器中

public class ScreenAdapter extends BaseAdapter {
    private Context context;
    private List<Experiment.DataBean.PlotsBean.PhenotypesBean> phenotypesBeanList;
    private LayoutInflater Inflater;
    public boolean flage = false;
    public Map<Integer, String> selected;
    private List<String> dataIds;

    public ScreenAdapter(Context context, List<Experiment.DataBean.PlotsBean.PhenotypesBean> phenotypesBeanList) {
        if (phenotypesBeanList != null) {
            this.phenotypesBeanList = phenotypesBeanList;
        } else {
            this.phenotypesBeanList = new ArrayList<>();
        }
        selected = new HashMap<>();
        dataIds = new ArrayList<>();
        this.context = context;
        Inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    }

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

    @Override
    public Experiment.DataBean.PlotsBean.PhenotypesBean getItem(int position) {
        return phenotypesBeanList.get(position);
    }

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

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder = null;
        if (convertView == null) {
            convertView = Inflater.inflate(R.layout.item_screen, null);
            viewHolder = new ViewHolder();
            x.view().inject(viewHolder, convertView);
            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }
        final Experiment.DataBean.PlotsBean.PhenotypesBean phenotypesBean = phenotypesBeanList.get(position);
        if (phenotypesBean != null) {
            viewHolder.textView.setText(phenotypesBeanList.get(position).getPhenotypeName());

            // 根据isSelected来设置checkbox的显示状况
            if (flage) {
                viewHolder.checkboxOperateData.setVisibility(View.VISIBLE);
            } else {
                viewHolder.checkboxOperateData.setVisibility(View.GONE);
            }

            if (selected.containsKey(position)) {
                viewHolder.checkboxOperateData.setChecked(true);
            } else {
                viewHolder.checkboxOperateData.setChecked(false);
            }
            viewHolder.checkboxOperateData.setChecked(phenotypesBean.isCheck);
            //注意这里设置的不是onCheckedChangListener,还是值得思考一下的
/*            viewHolder.checkboxOperateData.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (phenotypesBean.isCheck) {
                        phenotypesBean.isCheck = false;
                    } else {
                        phenotypesBean.isCheck = true;
                    }
                }
            });*/
        }
        return convertView;
    }

    class ViewHolder {
        @ViewInject(R.id.screen_checkbox)
        CheckBox checkboxOperateData;
        @ViewInject(R.id.text_screen)
        TextView textView;
    }
}

实体类

private int PhenotypeId;
private String PhenotypeName;
public boolean isCheck;
 
布局
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/activity_screen"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context="com.kenfeng.kfhjlr.kfzy.Activity.Activity.ScreenActivity">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:background="@color/zhuTi"
        android:orientation="vertical"
        android:padding="5dp">

        <TextView
            android:id="@+id/topBar_title"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_centerVertical="true"
            android:text="数据筛选"
            android:textColor="@color/white"
            android:textSize="24sp" />

        <ImageView
            android:id="@+id/back"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentLeft="true"
            android:layout_alignParentStart="true"
            android:layout_centerVertical="true"
            android:layout_marginLeft="10dp"
            android:layout_marginStart="10dp"
            android:src="@mipmap/back" />

        <TextView
            android:id="@+id/OK"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentEnd="true"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="15dp"
            android:text="确定"
            android:textColor="@color/white"
            android:textSize="20sp" />
    </RelativeLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <Button
            android:id="@+id/bt_editor"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="编辑"
            android:textSize="17sp" />

        <Button
            android:id="@+id/bt_all"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="全选"
            android:textSize="17sp" />

        <Button
            android:id="@+id/bt_invert"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="反选"
            android:textSize="17sp" />

        <Button
            android:id="@+id/bt_clean"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:text="清除"
            android:textSize="17sp" />


    </LinearLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="1dp"
        android:background="#D6D7D7" />

    <ListView
        android:id="@+id/listView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"></ListView>
</LinearLayout>
 
item的布局
<?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">

    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="50dp"
        android:layout_weight="4"
        android:gravity="center_vertical"
        android:orientation="horizontal">
        <CheckBox
            android:id="@+id/screen_checkbox"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:checked="false"
            android:clickable="true"
            android:focusable="false"
            android:gravity="center" />
        <TextView
            android:id="@+id/text_screen"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="10dp"
            android:text="脱粒时小区样品穗重"
            android:textSize="20sp" />
    </LinearLayout>
</LinearLayout>

 

参考源码 http://blog.csdn.net/zuiwuyuan/article/details/50042841http://blog.csdn.net/zuiwuyuan/article/details/50042841

http://blog.csdn.net/zuiwuyuan/article/details/50042841

http://blog.csdn.net/zuiwuyuan/article/details/50042841

http://blog.csdn.net/zuiwuyuan/article/details/50042841

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值