作者:菜牛 | 来源:互联网 | 2023-08-31 10:25
先说明接下来要实现的ListView+CheckBox支持的功能:
1. 外部点击“编辑”(长按ListView的某一项也可),出现复选框;
2. 支持全选、复选、全不选
3. 支持获取选中的数据的信息
下面是mainactivity的xml布局
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical">
android:layout_
android:layout_
android:orientation="horizontal">
android:id="@+id/listview"
android:layout_
android:layout_
>
下面是listview条目布局:
xmlns:tools="http://schemas.android.com/tools"
android:layout_
android:layout_
android:orientation="vertical">
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:gravity="center_vertical"
android:orientation="horizontal">
android:id="@+id/checkbox_operate_data"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:layout_margin="6dp"
/>
android:id="@+id/material_item_img"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="@drawable/ic_launcher" />
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:orientation="horizontal"
android:paddingBottom="10dp"
android:paddingTop="10dp">
android:id="@+id/user_head_img"
android:layout_
android:layout_
android:background="#4483c9" />
android:layout_
android:layout_
android:orientation="vertical">
android:id="@+id/text_title"
android:layout_
android:layout_
android:gravity="center_vertical"
android:singleLine="true"
android:text="标题"
android:textColor="#555555"
android:textSize="16sp" />
android:layout_
android:layout_
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:orientation="horizontal">
android:id="@+id/text_desc"
android:layout_
android:layout_
android:layout_gravity="center_vertical"
android:gravity="bottom"
android:singleLine="true"
android:text="描述描述描述描述描述描述"
android:textColor="#aaaaaa"
android:textSize="14sp" />
下面是bean类
package com.example.listview;
public class DataBean {
public String id;
public String title;
public String desc;
public boolean isCheck; //该属性主要标志CheckBox是否选中
public DataBean(String id, String title, String desc) {
this.id = id;
this.title = title;
this.desc = desc;
}
}
定义数据显示的Adapter,在该Adapter中,我们需要实现两个重要的功能:控制是否显示CheckBox; 控制CheckBox是否显示,代码中注释了这个 ,直接就是CheckBox显示的情况,想看隐藏的,把代码中的注释去掉即可,然后再xml中让CheckBox不显示。
package com.example.listview;
import java.util.List;
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.TextView;
public class MyAdapter extends BaseAdapter {
private Context mContext;
private List mDatas;
private LayoutInflater mInflater;
//public boolean flage = false;
public MyAdapter(Context mContext, List mDatas) {
this.mCOntext= mContext;
this.mDatas = mDatas;
mInflater = LayoutInflater.from(this.mContext);
}
@Override
public int getCount() {
// TODO Auto-generated method stub
return mDatas.size();
}
@Override
public Object getItem(int arg0) {
// TODO Auto-generated method stub
return mDatas.get(arg0);
}
@Override
public long getItemId(int arg0) {
// TODO Auto-generated method stub
return arg0;
}
@Override
public View getView(int position, View convertView, ViewGroup viewGroup) {
ViewHolder holder = null;
if (cOnvertView== null) {
holder = new ViewHolder();
// 条目布局
cOnvertView= mInflater.inflate(R.layout.listadata, null);
holder.checkboxOperateData = (CheckBox) convertView
.findViewById(R.id.checkbox_operate_data);
holder.textTitle = (TextView) convertView
.findViewById(R.id.text_title);
holder.textDesc = (TextView) convertView
.findViewById(R.id.text_desc);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
final DataBean dataBean = mDatas.get(position);
if (dataBean != null) {
holder.textTitle.setText(dataBean.title);
holder.textDesc.setText(dataBean.desc);
// 根据isSelected来设置checkbox的显示状况
//if (flage) {
// holder.checkboxOperateData.setVisibility(View.VISIBLE);
// } else {
// holder.checkboxOperateData.setVisibility(View.GONE);
// }
//
holder.checkboxOperateData.setChecked(dataBean.isCheck);
//注意这里设置的不是onCheckedChangListener,还是值得思考一下的
holder.checkboxOperateData.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (dataBean.isCheck) {
dataBean.isCheck = false;
} else {
dataBean.isCheck = true;
}
}
});
}
return convertView;
}
class ViewHolder {
public CheckBox checkboxOperateData;
public TextView textTitle;
public TextView textDesc;
}
、、、、下面是主代码
package com.example.listview;import java.util.ArrayList;import java.util.List;import android.os.Bundle;import android.support.v7.app.ActionBarActivity;import android.util.Log;import android.view.View;import android.widget.Button;import android.widget.ListView;import android.widget.Toast;public class MainActivity extends ActionBarActivity {private Button button; private ListView listView; private List mDatas; private MyAdapter mAdapter; @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);button = (Button) findViewById(R.id.edit1); listView = (ListView) findViewById(R.id.listview); mDatas = new ArrayList<>(); for (int i = 0; i <20; i++) { DataBean dataBean = new DataBean("" + i, "上邪", "山无棱,天地合,乃敢与君绝"); mDatas.add(dataBean); } mAdapter = new MyAdapter(this, mDatas); listView.setAdapter(mAdapter); }/** * 编辑、取消编辑 * @param view */ public void btnEditList(View view) { // mAdapter.flage = !mAdapter.flage; // // if (mAdapter.flage) { // button.setText("取消"); // } else { // button.setText("编辑"); // } // // mAdapter.notifyDataSetChanged(); } /** * 全选 * @param view */ public void btnSelectAllList(View view) { // if (mAdapter.flage) { for (int i = 0; i ids = new ArrayList<>(); // if (mAdapter.flage) { for (int i = 0; i
}