Android的ListView多选删除操作实现代码
作者:fonely__ 发布时间:2023-06-25 22:24:38
最近尝试做了个listview的多选demo,网上看其他人的例子感觉不是很难,自己动手做了下,各种细节问题,没那么简单啊。既然做了,简单写个笔记记录下。
练手demo,命名笔记乱,不要介意哦。
主界面布局activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/rootView"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.listchecked.MainActivity" >
<ListView
android:id="@+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
>
</ListView>
<LinearLayout
android:id="@+id/button_group"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/listView1"
android:layout_alignParentBottom="true"
android:orientation="vertical" >
<Button
android:id="@+id/del"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="删除" />
</LinearLayout>
</RelativeLayout>
列表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:orientation="horizontal"
android:descendantFocusability="blocksDescendants" >
<!--注意上面这个属性,很关键,不加会导致list无法响应OnItemClickListener中的事件-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
>
<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:focusable="false" />
<ImageView
android:id="@+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitCenter"
android:src="@drawable/ic_launcher" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Title"
android:textSize="20sp" />
<TextView
android:id="@+id/teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Teacher" />
<TextView
android:id="@+id/time"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Time" />
<TextView
android:id="@+id/peopleNum"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="peopleNum"
/>
</LinearLayout>
</LinearLayout>
这个item的布局就被在细节上坑了一次,item中添加button,checkbox,imageButton等时,会导致只响应这些控件的事件,item整体的事件就不会响应了,刚开始没有加那条属性,勾选checkbox后,点删除没有反应。这问题要发现也有点难度,之前见大神们推荐过分析布局的工具hierarchyviewer,这个工具,个人感觉和前端开发中的F12类似啊,很方便,可以看到每个布局的情况。也是好奇,点开一看,整个item的布局,从父布局,到子布局,只要checkbox可以接受click,其他全部是false,这就是问题所在了,后来百度了一下,即如上结论。
解决办法就是添加Android:descendantFocusability=”blocksDescendants” 到list的item的布局里,添加以后,发现checkbox还是可以被单独点击,不响应list的点击选中事件,很是奇怪,看其他人的例子中就没有这种现象。最后只能设置checkbox不能被点击 android:clickable=”false” 布局问题解决了,下面是java类的源码:
首先是bean类,ItemBean.java
package com.example.listchecked;
public class ItemBean {
private int imgRes;
private String title,teacher,time;
private int peopleNum,id;
private boolean isChecked;
private boolean isShow;
public boolean isShow() {
return isShow;
}
public void setShow(boolean isShow) {
this.isShow = isShow;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public boolean isChecked() {
return isChecked;
}
public void setChecked(boolean isChecked) {
this.isChecked = isChecked;
}
public int getImgRes() {
return imgRes;
}
public void setImgRes(int img) {
this.imgRes = img;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getTeacher() {
return teacher;
}
public void setTeacher(String teacher) {
this.teacher = teacher;
}
public String getTime() {
return time;
}
public void setTime(String time) {
this.time = time;
}
public int getPeopleNum() {
return peopleNum;
}
public void setPeopleNum(int peopleNum) {
this.peopleNum = peopleNum;
}
}
自定义的Adapter,MyListAdapter.java
package com.example.listchecked;
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.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.ImageView;
import android.widget.TextView;
public class MyListAdapter extends BaseAdapter
{
private LayoutInflater inflater;
private List<ItemBean> items;
private ItemBean item;
private OnShowItemClickListener onShowItemClickListener;
public MyListAdapter(List<ItemBean> list,Context context)
{
items=list;
inflater=LayoutInflater.from(context);
}
@Override
public int getCount() {
// TODO 自动生成的方法存根
return items.size();
}
@Override
public Object getItem(int position) {
// TODO 自动生成的方法存根
return items.get(position);
}
@Override
public long getItemId(int position) {
// TODO 自动生成的方法存根
return items.get(position).getId();
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO 自动生成的方法存根
ViewHolder holder;//使用ViewHolder,大神说能提升性能
if(convertView==null)
{
holder=new ViewHolder();
convertView=inflater.inflate(R.layout.item_view, null);
holder.img=(ImageView) convertView.findViewById(R.id.imageView1);
holder.cb=(CheckBox) convertView.findViewById(R.id.checkBox1);
holder.title=(TextView)convertView.findViewById(R.id.title);
holder.teacher=(TextView) convertView.findViewById(R.id.teacher);
holder.time=(TextView) convertView.findViewById(R.id.time);
holder.poeple=(TextView)convertView.findViewById(R.id.peopleNum);
convertView.setTag(holder);
}else
{
holder=(ViewHolder) convertView.getTag();
}
item=items.get(position);
if(item.isShow())
{
holder.cb.setVisibility(View.VISIBLE);
}
else
{
holder.cb.setVisibility(View.GONE);
}
holder.img.setImageResource(item.getImgRes());
holder.title.setText(item.getTitle());
holder.teacher.setText("主讲:"+item.getTeacher());
holder.time.setText("课时:"+item.getTime()+"讲");
holder.poeple.setText("学习人数:"+item.getPeopleNum());
holder.cb.setOnCheckedChangeListener(new OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked)
{
item.setChecked(true);
}
else
{
item.setChecked(false);
}
//回调方法,讲item加入已选择的
onShowItemClickListener.onShowItemClick(item);
}
});
//监听后设置选择状态
holder.cb.setChecked(item.isChecked());
return convertView;
}
static class ViewHolder
{
ImageView img;
CheckBox cb;
TextView title,teacher,time,poeple;
}
public interface OnShowItemClickListener {
public void onShowItemClick(ItemBean bean);
}
public void setOnShowItemClickListener(OnShowItemClickListener onShowItemClickListener) {
this.onShowItemClickListener = onShowItemClickListener;
}
}
最后是MainActivity.java
package com.example.listchecked;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;
import com.example.listchecked.MyListAdapter.OnShowItemClickListener;
public class MainActivity extends Activity implements OnShowItemClickListener {
private ListView listView;
private List<ItemBean> dataList,selectedList;
private MyListAdapter myAdapter;
private RelativeLayout rootView;
private LinearLayout menuView;
private LinearLayout openView;
private static boolean isShow;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
isShow=false;
setContentView(R.layout.activity_main);
Button delbtn=(Button) findViewById(R.id.del);
rootView=(RelativeLayout) findViewById(R.id.rootView);
menuView=(LinearLayout) findViewById(R.id.button_group);
listView=(ListView) findViewById(R.id.listView1);
dataList=new ArrayList<ItemBean>();
selectedList=new ArrayList<ItemBean>();
for(int i=0;i<10;i++)
{
ItemBean item=new ItemBean();
item.setId(i);
item.setImgRes(R.drawable.ic_launcher);
item.setTitle("第"+item.getId()+"个");
item.setTeacher("杨老师");
item.setTime("34");
item.setPeopleNum(i+1*100);
item.setChecked(false);
item.setShow(isShow);
dataList.add(item);
}
myAdapter=new MyListAdapter(dataList, this);
myAdapter.setOnShowItemClickListener(MainActivity.this);
listView.setAdapter(myAdapter);
delbtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
showMenu();
isShow=true;
selectedList.clear();
for(ItemBean item:dataList)
{
item.setShow(isShow);
}
myAdapter.notifyDataSetChanged();
}
});
listView.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO 自动生成的方法存根
if (isShow) {
ItemBean item = dataList.get(position);
boolean isChecked = item.isChecked();
if (isChecked) {
item.setChecked(false);
} else {
item.setChecked(true);
}
myAdapter.notifyDataSetChanged();
Log.d("select",selectedList.size()+"");
}
}
});
}
//显示选择删除等的菜单
private void showMenu()
{
RelativeLayout.LayoutParams lp=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
openView=(LinearLayout) inflater.inflate(R.layout.delmenu_layout, null);
rootView.removeView(menuView);
rootView.addView(openView,lp);
final Button sBtn=(Button) openView.findViewById(R.id.selectAll);
Button dBtn=(Button) openView.findViewById(R.id.del_button);
Button cBtn= (Button) openView.findViewById(R.id.cancel_button);
sBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if ("全选".equals(sBtn.getText().toString())) {
for (ItemBean bean : dataList) {
if (!bean.isChecked()) {
bean.setChecked(true);
if (!selectedList.contains(bean)) {
selectedList.add(bean);
}
}
}
myAdapter.notifyDataSetChanged();
sBtn.setText("反选");
} else if ("反选".equals(sBtn.getText().toString())) {
for (ItemBean bean : dataList) {
bean.setChecked(false);
if (!selectedList.contains(bean)) {
selectedList.remove(bean);
}
}
myAdapter.notifyDataSetChanged();
sBtn.setText("全选");
}
}
});
dBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if (selectedList!=null && selectedList.size()>0) {
dataList.removeAll(selectedList);
myAdapter.notifyDataSetChanged();
selectedList.clear();
} else {
Toast.makeText(MainActivity.this, "请选择条目", Toast.LENGTH_SHORT).show();
}
}
});
cBtn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO 自动生成的方法存根
if (isShow) {
selectedList.clear();
for (ItemBean bean : dataList) {
bean.setChecked(false);
bean.setShow(false);
}
myAdapter.notifyDataSetChanged();
isShow = false;
restoreView();
}
}
});
}
@Override
public void onShowItemClick(ItemBean bean) {
// TODO 自动生成的方法存根
if (bean.isChecked() && !selectedList.contains(bean)) {
selectedList.add(bean);
} else if (!bean.isChecked() && selectedList.contains(bean)) {
selectedList.remove(bean);
}
}
private void restoreView()
{
rootView.addView(menuView);
if(openView!=null)
{
rootView.removeView(openView);
openView=null;
}
}
}
最后还有那个小菜单的布局,还是放上吧
<?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="horizontal" >
<Button
android:id="@+id/selectAll"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="全选" />
<Button
android:id="@+id/del_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="删除" />
<Button
android:id="@+id/cancel_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="取消" />
</LinearLayout>
全部代码都放上了,如果不想复制粘贴,GitHub地址:https://github.com/2767321434/ListChecked
猜你喜欢
- 本文实例为大家分享了Android九宫格图片展示的具体代码,供大家参考,具体内容如下XmlHelperusing System.Xml;us
- 有项目需求需要绘制多个圆圈,并且使用连续的数字对其排列起来,也就是好多排的圆圈。首先看一下效果图:一排设置为8个,一共有53个的:一排设值为
- 方式一:例如:”0000123” (字符串必须全为数字)处理过程:String tempStr = "0000123";
- Maven打包没有指定主类在使用IDEA开发项目的时候经常会遇到使用Maven打包项目(打成jar包或者zip包),但是之前不太清楚打包插件
- 页眉位于文档中每个页面的顶部区域,常用于显示文档的附加信息,可以插入时间、图形、公司微标、文档标题、文件名或作者姓名等;页脚位于文档中每个页
- Spring是一个开放源代码的设计层面框架,他解决的是业务逻辑层和其他各层的松耦合问题,因此它将面向接口的编程思想贯穿整个系统应用。Spri
- 本文实例讲述了java使用dom4j生成与解析xml文档的方法。分享给大家供大家参考,具体如下:xml是一种新的数据格式,主要用于数据交换。
- java实现读取、删除文件夹下的文件package test.com;import java.io.File;import java.io.
- 对象持久化是指将内存中的对象保存到可永久保存的存储设备中(如磁盘)的一种技术。本文介绍的是除数据库之外的几种对象持久化方式。具体如下:保存成
- 最近安装了idea,觉得比eclipse好用很多,今天不知道为啥yml文件就不识别了,上面显示一个问号,我查了半天,解决办法就是安装一个插件
- JDK * ,代理接口没有实现类,实现 * JDK代理,代理的是接口,那么笔者想一想,既然代理的是接口,那如果没有实现类怎么办,能不能代
- 1、声明一个测试对象import java.time.LocalDate;import java.util.List;import lomb
- 在这篇文章中,我将向您展示如何用新的Java 8 forEach语句循环一个List和Map。1、forEach 和 Map1.1、常规循环
- 本文实例讲述了Android自定义dialog简单实现方法。分享给大家供大家参考,具体如下:@Override protected void
- 什么是EurekaEureka是Netfilx开源的一个用来实现微服务的注册与发现的组件。它包含Server和Client两部分。为什么要有
- 在使用Gateway 调用一个文件上传服务时 前端传来的File的base64字符串怎么都接受不到 但是用Body方式请求就能接收到后来经过
- 本文实例为大家分享了Java实现二分查找的变种,供大家参考,具体内容如下普通二分查找:先回顾一下普通的二分查找注意:二分查找有这样一个问题:
- 提示:IntelliJ IDEA以下简称IDEA;####IntelliJ IDEA 配置git:需要的材料:一、git.exe二、配置gi
- Java连接SQL Server数据库的详细操作流程一.明确JDK版本和下载驱动1.1 JDK版本查看win + r输入cmd,命令窗口输入
- 用idea编写代码不多天,写代码的时候突然左侧目录没了,遇到这种情况相信大多数的小伙伴都是和我一样直接百度,于是网上找了很长时间,大多数都是