版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
【移动应用开发技术】怎么在Android中利用ExpandableListView实现单选和多选
这期内容当中在下将会给大家带来有关怎么在Android中利用ExpandableListView实现单选和多选,文章内容丰富且以专业的角度为大家分析和叙述,阅读完这篇文章希望大家可以有所收获。activity_main.xml<LinearLayout
xmlns:android="/apk/res/android"
xmlns:tools="/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="/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="/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.javapublic
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");
Li
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 【道德与法治】山西省忻州市繁峙县2022-2023学年七年级下学期期中试题(解析版)
- 店铺转让经营权合同6篇
- 甲硝唑在皮肤溃疡治疗中的作用
- 合作:开启成功之门的钥匙
- 柔道运动皮肤传染病有哪些
- 职业压力干预反应的临床分析
- 高血压患者生活调养-评估指标监测
- 公共交通:乘客的健康安全
- 名人读书心语:阅读的力量与启迪
- 高血压专科管理及治疗:动态监测
- 蚕宝宝的一生(科学)-PPT课件
- 医脉通三级综合医院服务能力指南2016年版
- 2022年电缆检验工证高级证考试题
- 软件开发合同
- 物业的延伸服务细化
- 第六章 对映异构教学课件
- 细胞色素P450酶系总活性荧光定量检测试剂盒产品说明书
- 关于水果罐头推出市场策划方案
- 国家最新煤的发热量测定方法
- 麦肯锡——大数据:创新、竞争和生产力的下一个前沿
- 县地税局关于“岗位大练兵,业务大比武”活动总结报告
评论
0/150
提交评论