android checkbox 列表的使用
今天继续和大家分享涉及到listview的内容。在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作。比如在一个清单页面,我们需要记录用户勾选了哪些条目。这个的实现并不太难,但是有很多朋友来问我如何实现,他们有遇到各种各样的问题,这里就一并写出来和大家一起分享。
ListView的操作就一定会涉及到item在这里插入图片描述
和Adapter,我们还是先来实现这部分内容。
首先,写个item的xml布局,里面放置一个TextView和一个CheckBox。要注意的时候,这里我设置了CheckBox没有焦点,这样的话,无法单独点击checkbox,而是在点击listview的条目后,Checkbox会响应操作。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:tools="http://schemas.android.com/tools"
android:padding="10dp"
android:orientation="horizontal">
<TextView
android:id="@+id/tv_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:text="item"/>
<CheckBox
android:id="@+id/ck"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:theme="@style/MyCheckBox"/>
</RelativeLayout>
下面就写一个Adapter类,我们依然继承BaseAdapter类,。这里我们使用一个实体类CheckBean来记录checkbox在对应位置的选中状况,这是本例的实现的基础。
public class MyAdapter extends BaseAdapter {
Context mContext;
List<CheckBean> lists;
LayoutInflater inflater;
CheckBox ck;
private int testBool;
public MyAdapter (Context context, List<CheckBean> lists,CheckBox ck){
this.mContext = context;
this.lists = lists;
this.ck = ck;
inflater = LayoutInflater.from(context);
}
@Override
public int getCount() {
return lists.size();
}
@Override
public Object getItem(int position) {
return lists.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if (convertView == null) {
convertView = inflater.inflate(R.layout.item_check, parent, false);
holder = new ViewHolder();
holder.tv = convertView.findViewById(R.id.tv_title);
holder.ck = convertView.findViewById(R.id.ck);
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
//初始化
holder.tv.setText(lists.get(position).getTitle());
防止滑动时选中混乱//
if (holder.ck.isChecked()) { //选中和未选中状态
holder.ck.setButtonTintList(ColorStateList.valueOf(mContext.getResources().getColor(R.color.green)));
}else {
holder.ck.setButtonTintList(ColorStateList.valueOf(mContext.getResources().getColor(R.color.grey_dark)));
}
holder.ck.setId(position);
holder.ck.setChecked(lists.get(position).isCheck());
///
final ViewHolder finalHolder = holder;
holder.ck.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
CheckBean dat = lists.get(buttonView.getId());
dat.setCheck(isChecked);
lists.set(buttonView.getId(), dat);
这里做的是监听每个item是否全是true或者有false出现///
testBool = 0;
for (int i = 0; i < lists.size(); i++) {
if (lists.get(i).isCheck()) {
testBool++;
}
}
if (testBool == lists.size()) {
ck.setChecked(true);
}else {
ck.setChecked(false);
}
/
if (isChecked) {
finalHolder.ck.setButtonTintList(ColorStateList.valueOf(mContext.getResources().getColor(R.color.green)));
} else {
finalHolder.ck.setButtonTintList(ColorStateList.valueOf(mContext.getResources().getColor(R.color.grey_dark)));
}
if (finalHolder.ck.isChecked() ) {
finalHolder.ck.setChecked(true);
dat.setCheck(true);
} else {
finalHolder.ck.setChecked(false);
dat.setCheck(false);
}
}
});
return convertView;
}
public class ViewHolder{
TextView tv;
CheckBox ck;
}
public void ckAllVertical(boolean isCheck){
for (int i = 0; i < lists.size(); i++) {
lists.get(i).setCheck(isCheck);
}
this.notifyDataSetChanged();
}
}
到这里我们的adapter就已经做好了需要注意的是如何和MainActivity进行关联,无非是在MainActivity添加一个checkBox,main_activity布局如下:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="20dp">
<CheckBox
android:id="@+id/ck_All"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="2dp"
android:text="全选"
android:theme="@style/MyCheckBox" />
</LinearLayout>
<ListView
android:id="@+id/list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
最后来看看我们的主布局代码是如何关联adapter 中的item中的checkBox的:
package com.shenzhen.honpe.singlecheckboxlistdemo;
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.ListView;
import com.shenzhen.honpe.singlecheckboxlistdemo.adapter.MyAdapter;
import com.shenzhen.honpe.singlecheckboxlistdemo.bean.CheckBean;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener {
private List<CheckBean> mLists;
private ListView listView;
private MyAdapter adapter;
private CheckBox ckAll;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.list_view);
ckAll = findViewById(R.id.ck_All);
initData();
}
private void initData() {
mLists = new ArrayList<>();
for (int i = 0; i < 20; i++) {
CheckBean bean = new CheckBean(false, "item"+(i+1));
mLists.add(bean);
}
adapter = new MyAdapter(this,mLists,ckAll);
listView.setAdapter(adapter);
ckAll.setOnCheckedChangeListener(this);
adapter.notifyDataSetChanged();
}
@Override
public void onCheckedChanged(CompoundButton buttonView, final boolean isChecked) {
ckAll.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
adapter.ckAllVertical(isChecked);
}
});
}
}