这段时间在些项目时发现一个小技术点还是挺有意思的,特来总结一下,就是如何在ListView的item中自己写了一个checkbox,那么如何获取到这个item的checkbox的选中事件,进而怎样操作,比方说点击全选、取消后如何让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"
android:padding="25dp"
android:layout_centerVertical="true">
<ImageView
android:id="@+id/iv_app_icon"
android:src="@mipmap/ic_launcher"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_centerVertical="true"/>
<TextView
android:id="@+id/tv_app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="@id/iv_app_icon"
android:layout_centerVertical="true"
android:text="微信"
android:paddingLeft="18dp"
android:textSize="15dp"/>
<CheckBox
android:id="@+id/cb_app_uninstall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:focusable="false"
android:clickable="false"/>
</RelativeLayout>
大家可以看到我将checkbox命名为cb_app_uninstall,其实主要是一个与卸载相关的选中键,然后我将adapter与holder提取出来单独封装为一个类
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import java.util.ArrayList;
//这个是自定义的adpter的基类
public class MyBaseAdapter<T> extends BaseAdapter {
private ArrayList<T> dataList;
private Context mContext;
LayoutInflater inflater;
public MyBaseAdapter(Context context,ArrayList<T> dataList){
this.mContext = context;
this.dataList = dataList;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
@Override
public int getCount() {
return dataList.size();
}
@Override
public Object getItem(int position) {
return dataList.get(position);
}
@Override
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
return null;
}
}
下面是我单独提取出来的AppManagerAdapter
import android.content.Context;
import android.view.View;
import android.view.ViewGroup;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.qingcheng.mobilemanager.R;
import com.qingcheng.mobilemanager.bean.AppInfo;
import com.qingcheng.mobilemanager.holder.AppViewholder;
import java.util.ArrayList;
import java.util.List;
public class AppManagerAdapter extends MyBaseAdapter<AppInfo> {
private Context mContext;
private ArrayList<AppInfo> mDatas;
public List<Boolean> mChecked;
public AppManagerAdapter(Context context, ArrayList<AppInfo> dataList) {
super(context, dataList);
this.mContext = context;
this.mDatas = dataList;
mChecked = new ArrayList<Boolean>();
for (int i = 0; i < dataList.size(); i++){
mChecked.add(false);
}
}
@Override
public AppInfo getItem(int position) {
return mDatas.get(position);
}
@Override
public View getView(final int position, View convertView, ViewGroup parent) {
AppViewholder holder ;
if (convertView == null){
convertView = View.inflate(mContext,R.layout.list_item_app_manager,null);
holder = new AppViewholder();
holder.ivAppIcon = (ImageView)convertView.findViewById(R.id.iv_app_icon);
holder.tvAppName = (TextView) convertView.findViewById(R.id.tv_app_name);
holder.cbAppUninstall = (CheckBox) convertView.findViewById(R.id.cb_app_uninstall);
convertView.setTag(holder);
}else{
holder = (AppViewholder) convertView.getTag();
}
AppInfo info = getItem(position);
holder.ivAppIcon.setImageDrawable(info.appIcon);
holder.tvAppName.setText(info.appName);
holder.cbAppUninstall.setChecked(mChecked.get(position));
return convertView;
}
}
BaseHolder是holder的基类
import android.view.View;
public abstract class BaseHolder<T> {
public View convertView;
public T mData;
public BaseHolder(){
convertView = initView();
convertView.setTag(this);
}
public abstract View initView();
public void setData(T data){
mData = data;
}
}
这个是我抽取出来的AppViewholder
import android.view.View;
import android.widget.CheckBox;
import android.widget.ImageView;
import android.widget.TextView;
import com.qingcheng.mobilemanager.R;
import com.qingcheng.mobilemanager.bean.AppInfo;
import com.qingcheng.mobilemanager.utils.UiUtils;
public class AppViewholder extends BaseHolder<AppInfo> {
public ImageView ivAppIcon;
public TextView tvAppName;
public CheckBox cbAppUninstall;
@Override
public View initView() {
View view = UiUtils.inflate(R.layout.list_item_app_manager);
ivAppIcon = (ImageView) view.findViewById(R.id.iv_app_icon);
tvAppName = (TextView) view.findViewById(R.id.tv_app_name);
cbAppUninstall = (CheckBox) view.findViewById(R.id.cb_app_uninstall);
return view;
}
@Override
public void setData(AppInfo data) {
super.setData(data);
tvAppName.setText(mData.appName);
}
}
好了,现在来说说我们的正事,如何获取listview中checkbox的选中事件
首先不知道大家有没有注意到我在AppManagerAdapter中设置了一个
public List<Boolean> mChecked;
mChecked集合 就是一切的关键
//先在AppManagerAdapter中定义mChecked的长度并赋值
mChecked = new ArrayList<Boolean>();
for (int i = 0; i < dataList.size(); i++){
mChecked.add(false);
}
然后在getView中set
holder.cbAppUninstall.setChecked(mChecked.get(position));
再在Activity中对listView的item获取点击事件,先要对listView设置监听,并对checkbox的点击事件设置为
android:focusable="false"
android:clickable="false"
如果不这么设置会让listView的item得不到点击事件
/**
* 每个item被点击监听
*/
lvAppDisplay.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (mAdapter.mChecked.get(position)){
mAdapter.mChecked.set(position,false);
}else if (!mAdapter.mChecked.get(position)){
mAdapter.mChecked.set(position,true);
}
mAdapter.notifyDataSetChanged();
}
});
这样就可以及时获取checkbox的点击事件并及时更新控制他们的mChecked的状态
当想要点击一个类似全选的按钮达到全选的效果时可以这样做
private boolean checkedAll = false;
/**
* 全选事件
*/
tvCheckAll.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
if (!checkedAll){
for (int i = 0; i < mDatas.size(); i++){
mAdapter.mChecked.set(i,true);
}
checkedAll = true;
mAdapter.notifyDataSetChanged();
}else{
for (int i = 0; i < mDatas.size(); i++){
mAdapter.mChecked.set(i,false);
}
checkedAll = false;
mAdapter.notifyDataSetChanged();
}
}
});
这样就完美实现了在全选和全部取消之间的切换,从而我们就可以另一个方法中获取被选中item的position
for (int i = 0;i< mAdapter.mChecked.size();i++){
if (mAdapter.mChecked.get(i)){
//listItemID为被选中item的position
listItemID.add(i);
}
}