Android开发:BaseExpandableListAdapter的使用
项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现效果如下:
下面是实现步骤。
1、新建类PhoneListItem,用于表示分组中的每一个手机号码。
package com.ydtf.android;
public class PhoneListItem {
public String phone,name;
public boolean checked;
public PhoneListItem(String _name,String _phone,boolean _checked){
name=_name;
phone=_phone;
checked=_checked;
}
}
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:orientation="horizontal" android:minHeight="40px"
android:layout_gravity="center_vertical">
<CheckBox android:id="@+id/phone_check" android:focusable="false"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:layout_marginLeft="35px" android:checked="false"/>
<TextView android:id="@+id/phone_name" android:layout_width="80dip"
android:layout_height="wrap_content" android:layout_gravity="center_vertical" />
<TextView android:id="@+id/phone_number" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:textColor="?android:attr/textColorPrimary"
android:paddingLeft="10px" android:layout_gravity="center_vertical" />
</LinearLayout>
3、新建类PhoneGroup,用于表示一个分组。
import java.util.List;
public class PhoneGroup {
public String title;
private boolean checked;
public List<PhoneListItem> children;
public PhoneGroup(String title,boolean checked,List<PhoneListItem> children){
this.title=title;
setChecked(checked);
this.children=children;
}
public boolean getChecked(){
return checked;
}
public void setChecked(boolean b){
checked=b;
if(children!=null&&children.size()>0){//若children不为空,循环设置children的checked
for(PhoneListItem each : children){
each.checked=checked;
}
}
}
}
4、新建Activity布局文件phone_browser.xml,用于定义通讯簿的展现页面。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<ExpandableListView android:id="@+id/phonelist"
android:layout_width="fill_parent" android:layout_height="0dip"
android:layout_weight="1" />
</LinearLayout>
5、新建类QxtPhoneSelect,用于呈现通讯簿的Activity页面。
package com.ydtf.android;
import java.util.ArrayList;
import java.util.List;
import com.ydtf.android.PhoneGroupAdapter.ExpandableListHolder;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.ExpandableListView;
public class QxtSelectPhone extends Activity implements
ExpandableListView.OnGroupClickListener,ExpandableListView.OnChildClickListener{
private List<PhoneGroup> groups;
private PhoneGroupAdapter exlist_adapter = null;
private ExpandableListView exlist;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//加载layout
setContentView(R.layout.phone_browser);
//取得listview
exlist = (ExpandableListView) findViewById(R.id.phonelist);
//调用init方法,这个方法主要是,初始化一些数据
init();
//构建expandablelistview的适配器
exlist_adapter = new PhoneGroupAdapter(this, groups);
exlist.setOnChildClickListener(this);
exlist.setAdapter(exlist_adapter); //绑定视图-适配器
}
private void init() {
groups = new ArrayList<PhoneGroup>();
//构建List用作group1的子项
List<PhoneListItem> group1_children = new ArrayList<PhoneListItem>();
//往List中添加内容
PhoneListItem item = new PhoneListItem("和文明","1308763994", false);
group1_children.add(item);
item = new PhoneListItem("黄文明","1308763994", false);
group1_children.add(item);
item = new PhoneListItem("王文明","1308763994", false);
group1_children.add(item);
//拼装成 PhoneGroup
PhoneGroup phonegroup1=new PhoneGroup("group1",false,group1_children);
//------把前面的代码复制一遍,再添加一个组group2
//构建List用作group2的子项
List<PhoneListItem> group2_children = new ArrayList<PhoneListItem>();
//往List中添加内容
item = new PhoneListItem("张文明","1589065423", false);
group2_children.add(item);
item = new PhoneListItem("李文明","1589065423", false);
group2_children.add(item);
item = new PhoneListItem("赵文明","1589065423", false);
group2_children.add(item);
//拼装成 PhoneGroup
PhoneGroup phonegroup2=new PhoneGroup("group2",false,group2_children);
//添加进groups数组
groups.add(phonegroup1);
groups.add(phonegroup2);
}
//当分组行背点击时,让分组呈现“选中/取消选中”状态。
@Override
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
PhoneGroupAdapter.ExpandableListHolder holder=(ExpandableListHolder) v.getTag();
holder.chkChecked.setChecked(!holder.chkChecked.isChecked());
groups.get(groupPosition).children.get(childPosition).checked=
!groups.get(groupPosition).children.get(childPosition).checked;
return false;
}
@Override
public boolean onGroupClick(ExpandableListView parent, View v,
int groupPosition, long id) {
// groups.get(groupPosition).setChecked(!groups.get(groupPosition).getChecked());
// exlist_adapter.notifyDataSetChanged();
//
return false;
}
}
Android开发:BaseExpandableListAdapter的使用相关推荐
- Android开发 BaseExpandableListAdapter的使用
分享一下我老师大神的人工智能教程!零基础,通俗易懂!http://blog.csdn.net/jiangjunshow 也欢迎大家转载本篇文章.分享知识,造福人民,实现我们中华民族伟大复兴! 项目需要 ...
- 做简单的android 软件推荐,Android_适用于Android开发的简单聊天软件,适用于android 开发。是一个简 - phpStudy...
适用于Android开发的简单聊天软件 适用于android 开发.是一个简单的聊天软件,包括知识点,各个控件的运用(ExpandableListView,ViewPager,Spinner,Line ...
- android中资源文件的两种访问方式,在android开发中进行数据存储与访问的多种方式介绍...
在android开发中进行数据存储与访问的多种方式介绍 更新时间:2013年06月07日 16:24:23 作者: 很多时候我们的软件需要对处理后的数据进行存储或再次访问,Android为数据存储 ...
- Android开发系列(十二) QQ联系人列表升级版——ListView和ScrollView高阶使用方法...
今天继续进行QQ界面的开发工作.前一段时间讲过ExpandableListView的使用并且设置了一个比较简单的具有子菜单效果的联系人列表,本节添加进ScrollView控件,对QQ2013版的联系人 ...
- Android开发实战《手机安全卫士》——11.“进程管理”模块拓展 窗体小部件 生成快捷方式
文章目录 1.进程管理--隐藏系统进程 2.进程管理--锁屏清理 3.拓展功能--生成快捷方式 4.高级工具--常用号码查询(布局实现) 5.高级工具--常用号码查询(逻辑实现) 6.拓展功能--窗体 ...
- Android开发_android界面效果全汇总
(一)Activity页面切换的效果 先介绍下左右滑动切换Activity,对于复杂的手势原理一样,具体后述. 主要原理为监控触屏事件和手势事件,在触屏事件处理函数中调用手势事件处理函数,表示用户触屏 ...
- Android环境搭建和Android HelloWorld—Android开发环境搭建
Android_Android开发环境搭建-搭建Android的开发环境 1.我考虑先下载JDK7.0,在JDK的安装中,考虑一般SDK都是向上兼容的,于是选择了最高的版本7.0 这里是我总结的详细的 ...
- java安卓开发工具_推荐几个非常实用的Android开发工具
工欲善其事,必先利其器.我们进行Android开发也要有好的开发工具辅助才能更好更高效的完成各种开发,为用户提供更实用的应用程序.本文就为大家推荐几个非常实用的Android开发工具,及几个常用的编辑 ...
- android 网络编程实现,Android开发使用HttpURLConnection进行网络编程详解【附源码下载】...
本文实例讲述了Android开发使用HttpURLConnection进行网络编程.分享给大家供大家参考,具体如下: --HttpURLConnection URLConnection已经可以非常方便 ...
最新文章
- Java枚举类使用方式
- Mila唐建专访:Bengio领路,缘起生物梦,AI如何变革生物制药?
- poj2418map或者字典树
- Stanford NLP
- Java并发编程—为什么 wait() 方法需要写在 while 里,而不是 if?
- 感知算法论文(四):Mask Scoring R-CNN (2019)译文
- python爬取会议论文pdf_【python2.7】爬取知网论文
- LeetCode 93. 复原IP地址(回溯)
- 机智云获取树莓派传来的数据_哪些数据对云来说太冒险了?
- 项目管理中如何制定项目目标
- Excel 相同名称或ID的 内容 合并起来 同列不同内容剃加
- 【好用的办公软件】万彩办公大师教程丨标准安装版/绿色免安装版/离线版区别
- 2021中大厂php+go面试题(2)
- 重构于 Vite:我如何做 SSG、静态资源发布以及自动化部署
- Flowing Paint ——感受抽象
- 7-55 吉老师的回归 (15 分)
- Jstorm基础知识
- Android需求2 -- MTK手机内存显示和清除后台
- HTML:在浏览器页面上简单实现显示标准系统时间
- open BMC开发(三) IPMI功能配置添加
热门文章
- svn客户端,重新输入用户名密码
- (Linux)SVN客户端查看及重置账号密码
- android手机舆情分析,基于Android移动互联网舆情管理关键技术研究
- 动态规划-leetcode#115-不同的子序列
- tcp_tw_reuse、tcp_tw_recycle和tcp_timestamps
- 学术界为啥不爱投中文期刊?
- Unicode编码规范(摘抄)
- 【3D视觉创新应用竞赛作品系列​】牲畜体表信息的三维重建,代码详尽
- JS 限制文本框只能输入数字
- 全景软件拼接完全景图后的拼接痕迹如何用PS处理?