Android ExpandableListView单选以及多选实现代码
作者:不年少还青春 发布时间:2023-01-14 20:45:51
标签:Android,单选,多选
一、概述
ExpandableListView是常用的一个控件,今天自己做了个小练习,主要需求是单选以及多选的实现,看似比较简单,但是还是比较复杂,把代码贴给大家,有这种需求的可以参考一下。
二、效果截图
三、实现过程
activity_main.xml
<LinearLayout 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:orientation="vertical"
tools:context=".MainActivity" >
<ExpandableListView
android:id="@+id/exlistview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:listSelector="@android:color/transparent" >
</ExpandableListView>
</LinearLayout>
group_item.xml
<?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="match_parent"
android:descendantFocusability="blocksDescendants"
android:padding="10dp" >
<TextView
android:id="@+id/id_group_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:padding="10dp"
android:text="hao"
android:textColor="@android:color/black"
android:textIsSelectable="true"
android:textSize="15sp" >
</TextView>
<CheckBox
android:id="@+id/id_group_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
listview_item.xml
<?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="match_parent"
android:descendantFocusability="blocksDescendants"
android:padding="10dp" >
<TextView
android:id="@+id/id_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp"
android:layout_marginLeft="30dp"
android:textColor="#55acac"
android:textIsSelectable="true"
android:textSize="15sp" >
</TextView>
<CheckBox
android:id="@+id/id_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="false"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
MainAcitivity.java
public class MainActivity extends Activity {
private List<Map<String, String>> parentList = new ArrayList<Map<String, String>>();
private List<List<Map<String, String>>> childData = new ArrayList<List<Map<String, String>>>();
private ExpandableListView exListView;
private Context context = this;
private MyAdapter adapter;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
initData();
setListener();
}
/**
* 记录正在选中的子listview的item条目 用hashset是为了去除重复值
*/
private HashSet<String> hashSet;
private void setListener()
{
exListView.setOnGroupExpandListener(new OnGroupExpandListener()
{
@Override
public void onGroupExpand(int groupPosition)
{
//存取已选定的集合
hashSet = new HashSet<String>();
}
});
// ExpandableListView的Group的点击事件
exListView.setOnGroupClickListener(new OnGroupClickListener()
{
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id)
{
// 可以写点击后实现的功能
return false;
}
});
// ExpandableListView的child的点击事件
exListView.setOnChildClickListener(new OnChildClickListener()
{
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id)
{
Map<String, String> map = childData.get(groupPosition).get(
childPosition);
String childChecked = map.get("isChecked");
if ("No".equals(childChecked))
{
map.put("isChecked", "Yes");
hashSet.add("选定" + childPosition);
} else
{
map.put("isChecked", "No");
if (hashSet.contains("选定" + childPosition))
{
hashSet.remove("选定" + childPosition);
}
}
System.out.println("选定的长度==1" + hashSet.size());
System.out.println("选定的长度==2"
+ childData.get(groupPosition).size());
if (hashSet.size() == childData.get(groupPosition).size())
{
parentList.get(groupPosition).put("isGroupCheckd", "Yes");
} else
{
parentList.get(groupPosition).put("isGroupCheckd", "No");
}
adapter.notifyDataSetChanged();
return false;
}
});
}
// 初始化数据
private void initData()
{
for (int i = 0; i < 10; i++)
{
Map<String, String> groupMap = new HashMap<String, String>();
groupMap.put("groupText", "item" + i);
groupMap.put("isGroupCheckd", "No");
parentList.add(groupMap);
}
for (int i = 0; i < 10; i++)
{
List<Map<String, String>> list = new ArrayList<Map<String, String>>();
for (int j = 0; j < 4; j++)
{
Map<String, String> map = new HashMap<String, String>();
map.put("childItem", "childItem" + j);
map.put("isChecked", "No");
list.add(map);
}
childData.add(list);
}
adapter = new MyAdapter();
exListView.setAdapter(adapter);
exListView.expandGroup(0);
hashSet = new HashSet<String>();
}
private void initView()
{
exListView = (ExpandableListView) findViewById(R.id.exlistview);
}
/**
* 适配adapter
*/
private class MyAdapter extends BaseExpandableListAdapter {
@Override
public Object getChild(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childData.get(groupPosition).get(childPosition);
}
@Override
public long getChildId(int groupPosition, int childPosition)
{
// TODO Auto-generated method stub
return childPosition;
}
@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.listview_item,
null);
holder.childText = (TextView) convertView
.findViewById(R.id.id_text);
holder.childBox = (CheckBox) convertView
.findViewById(R.id.id_checkbox);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.childText.setText(childData.get(groupPosition)
.get(childPosition).get("childItem"));
String isChecked = childData.get(groupPosition).get(childPosition)
.get("isChecked");
if ("No".equals(isChecked))
{
holder.childBox.setChecked(false);
} else
{
holder.childBox.setChecked(true);
}
return convertView;
}
@Override
public int getChildrenCount(int groupPosition)
{
// TODO Auto-generated method stub
return childData.get(groupPosition).size();
}
@Override
public Object getGroup(int groupPosition)
{
return parentList.get(groupPosition);
}
@Override
public int getGroupCount()
{
// TODO Auto-generated method stub
return parentList.size();
}
@Override
public long getGroupId(int groupPosition)
{
// TODO Auto-generated method stub
return groupPosition;
}
@Override
public View getGroupView(final int groupPosition,
final boolean isExpanded, View convertView, ViewGroup parent)
{
ViewHolder holder = null;
if (convertView == null)
{
holder = new ViewHolder();
convertView = View.inflate(context, R.layout.group_item, null);
holder.groupText = (TextView) convertView
.findViewById(R.id.id_group_text);
holder.groupBox = (CheckBox) convertView
.findViewById(R.id.id_group_checkbox);
convertView.setTag(holder);
} else
{
holder = (ViewHolder) convertView.getTag();
}
holder.groupText.setText(parentList.get(groupPosition).get(
"groupText"));
final String isGroupCheckd = parentList.get(groupPosition).get(
"isGroupCheckd");
if ("No".equals(isGroupCheckd))
{
holder.groupBox.setChecked(false);
} else
{
holder.groupBox.setChecked(true);
}
/*
* groupListView的点击事件
*/
holder.groupBox.setOnClickListener(new OnClickListener()
{
@Override
public void onClick(View v)
{
CheckBox groupBox = (CheckBox) v
.findViewById(R.id.id_group_checkbox);
if (!isExpanded)
{
//展开某个group view
exListView.expandGroup(groupPosition);
} else
{
//关闭某个group view
exListView.collapseGroup(groupPosition);
}
if ("No".equals(isGroupCheckd))
{
exListView.expandGroup(groupPosition);
groupBox.setChecked(true);
parentList.get(groupPosition).put("isGroupCheckd",
"Yes");
List<Map<String, String>> list = childData
.get(groupPosition);
for (Map<String, String> map : list)
{
map.put("isChecked", "Yes");
}
} else
{
groupBox.setChecked(false);
parentList.get(groupPosition)
.put("isGroupCheckd", "No");
List<Map<String, String>> list = childData
.get(groupPosition);
for (Map<String, String> map : list)
{
map.put("isChecked", "No");
}
}
notifyDataSetChanged();
}
});
return convertView;
}
@Override
public boolean hasStableIds()
{
return true;
}
@Override
public boolean isChildSelectable(int groupPosition, int childPosition)
{
return true;
}
}
private class ViewHolder {
TextView groupText, childText;
CheckBox groupBox, childBox;
}
}
四、总结及注意点
1、设置CheckBox的点击事件,而非别的
2、exListView.collapseGroup(groupPosition); 关闭正展开的子ListView.
这是demo地址,欢迎下载:
Demo下载地址
来源:https://blog.csdn.net/Hello201404/article/details/48546387


猜你喜欢
- SpringBoot打jar包遇到的xml文件丢失在pom.xml的build标签中添加如下内容指定资源路径<resources>
- Unsafe类介绍第一次看到这个类时被它的名字吓到了,居然还有一个类自名Unsafe?读完本文,大家也能发现Unsafe类确实有点不那么安全
- 前言本文主要介绍了关于java结合keytool实现非对称签名和验证的相关内容,分享出来供大家参考学习,下面话不多说了,来一起看看详细的介绍
- springboot微服务内置了tomcat,在工程目录下执行:mvn clean package,可以将项目打成jar,通过java -j
- 最近项目中使用了 MyBatis-Plus,点击看官方文档。使用一个新的框架,首先是验证框架的使用。 使用 MyBatis-Plu
- 读取Java文件到byte数组的三种方法(总结)package zs;import java.io.BufferedInputStream;
- Java自定义注解一般使用场景为:自定义注解+ * 或者AOP,使用自定义注解来自己设计框架,使得代码看起来非常优雅。本文将先从自定义注解的
- springboot 高版本后不支持log4j了,很多人还是喜欢log4j风格的日志,我们自己来加载log4j,其实
- Java语言的垃圾回收1.垃圾回收机制的基本概念问:1.什么是Java垃圾回收?答:在Java语言的生命周期中,Java运行环境提供了一个系
- springmvc的图片上传1.导入相应的pom依赖 <dependency> <groupId>co
- 主从表关联查询,返回对象带有集合属性昨天有同事让我帮着看一个问题,mybatis主从表联合查询,返回的对象封装集合属性。我先将出现的问题记录
- 本文以实例介绍了C#如何通过winmm.dll来播放声音,主要实现步骤如下:1.首先导入如下两个函数:/// <summary>
- 一、项目简述本系统功能包括: 一款基于Springboot+Vue的电商项目,前后端分离项目,前台后台都有,前台商品展示购买,购物车分类,订
- 本文实例为大家分享了Android SeekBar实现平滑滚动的具体代码,供大家参考,具体内容如下由于项目需要,SeekBar只需要三个档,
- 本文实例为大家分享了Java测试网络连通性的方法,供大家参考,具体内容如下第一种方式:利用java运行时: Java代码 /** * tes
- Springboot 整合 RocketMQ 收发消息创建springboot项目pom.xml添加rocketmq-spring-boot
- C#客户端程序,生成后是一个exe,如果带有大量的dll,那么dll和exe会混乱在一起,看起来非常混乱,我们可以建立一个文件夹,把dll放
- 正文关于Java中的 * ,我们首先需要了解的是一种常用的设计模式--代理模式,而对于代理,根据创建代理类的时间点,又可以分为静态代理和动
- 1.如图所示,Spring配置文件应该带有是树叶标识,但此处显示的为普通的properties文件2.选择Open Module Setti
- 本文实例讲述了Android显示网络图片的方法,分享给大家供大家参考。具体方法如下:一般来说,在Android中显示一张网络图片其实是非常简