ListView结合CheckBox实现多选与记录

时间:2021-08-28 10:09:04

ListView结合Checkbox在安卓开发中是挺常见的一个项目需求,相信大多数开发者都遇到过类似的问题,今天我把做项目中的具体例子贴出来跟大家分享下,供大家学习交流,需求比较简单就不在这里赘述了,下面贴上代码供大家参考。


布局比较简单,一个TextView跟一个Checkbox

<?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="vertical">

<RelativeLayout
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="50dp">
<TextView
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选项"
android:id="@+id/value"
/>
<CheckBox
android:id="@+id/checkbox"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>

</LinearLayout>



自定义Adapter继承BaseAdapter,在Adapter处理Checkbox选中逻辑

/**
* Created by 谢栋 on 2017/1/20.
*/
public class UserSelectAdapter extends BaseAdapter{

private List<Name_Value> name_values;
private LayoutInflater inflater;
private static HashMap<Integer,Boolean> isSelected; // 用来控制CheckBox的选中状况


public UserSelectAdapter(Context context,List<Name_Value> name_values) {
this.name_values = name_values;
inflater = LayoutInflater.from(context);
isSelected = new HashMap<Integer, Boolean>();
// 初始化数据
initDate();

}
// 初始化isSelected的数据
private void initDate(){

MyL.e(name_values.size()+"-------");
for(int i=0; i<name_values.size();i++) {
getIsSelected().put(i,false);
}
}
@Override
public int getCount() {
return name_values.size();
}

@Override
public Object getItem(int position) {
return name_values.get(position);
}

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

@Override
public View getView(int position, View convertView, ViewGroup parent) {

ViewHolder holder = null;
if(convertView==null){
holder = new ViewHolder(); // 获得ViewHolder对象

// 导入布局并赋值给convertview
convertView = inflater.inflate(R.layout.user_multi_select_item,null);
holder.value = (TextView) convertView.findViewById(R.id.value);
holder.checkBox = (CheckBox) convertView.findViewById(R.id.checkbox);

convertView.setTag(holder); // 为view设置标签

}else {
// 取出holder
holder = (ViewHolder) convertView.getTag();
}


// 设置list中TextView的显示
holder.value.setText(name_values.get(position).getName());
// 根据isSelected来设置checkbox的选中状况
holder.checkBox.setChecked(getIsSelected().get(position));

return convertView;
}



class ViewHolder{
TextView value;
CheckBox checkBox;
}


/**
* 得到checkbox的选中状态
* @return
*/
public static HashMap<Integer, Boolean> getIsSelected() {
return isSelected;
}

public static void setIsSelected(HashMap<Integer, Boolean> isSelected) {
UserSelectAdapter.isSelected = isSelected;
}
}



然后在功能代码中具体处理listview中的Checkbox的选中状态,单选,多选,全选等逻辑

/**
* Created by 谢栋 on 2017/1/20.
*/
public class SearchDisplay extends Fragment{
private View view;
private ListView listview;
private List<Name_Value> name_values;
private UserSelectAdapter adapter;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.search_display_listview,null);

listview = (ListView)view.findViewById(R.id.listview);
Bundle bundle = getArguments();
name_values = (List<Name_Value>) bundle.getSerializable("name_value");
MyL.e(name_values.size()+"Fram----");
adapter = new UserSelectAdapter(getActivity(),name_values);
listview.setAdapter(adapter);



// 全选按钮的回调接口
view.findViewById(R.id.select_all).setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
// 遍历list的长度,将已选的按钮设为未选
for (int i = 0; i < name_values.size(); i++) {
adapter.getIsSelected().put(i, true);
}
// 刷新listview的显示
adapter.notifyDataSetChanged();

}
});

return view;
}
}


考虑到代码中有些项目隐私代码,上述代码中可能删除掉部分代码,但是不影响整体逻辑跟功能,写的不好的地方还请各位大牛们勿喷!