- 浏览: 920867 次
- 性别:
- 来自: 重庆
文章分类
- 全部博客 (453)
- Windows phone 7 (0)
- Andriod (47)
- iPhone (1)
- Web (30)
- J2EE (34)
- stucts 2学习笔记 (34)
- 开发项目 (3)
- spring学习笔记 (24)
- EJB学习笔记 (6)
- Hibernate学习笔记 (15)
- JPA学习笔记 (8)
- Jsp (11)
- ajax (7)
- 异常收集模块 (1)
- jquery (2)
- phoneGap (2)
- jquery Mobile (0)
- java面试总结 (5)
- Object-C (0)
- javascript (6)
- Eclipse (5)
- 支付集成 (2)
- Weblogic (1)
- Dubbox (5)
- Redis (10)
- linux (21)
- Codis (2)
- React Native (0)
- Mysql (6)
- Docker (3)
- 自动化部署 (1)
- 项目Bug管理平台 (2)
- 负载均衡 (1)
- Mycat (2)
- Java基础知识 (16)
- 数据库 (7)
- Maven (17)
- Kafka (21)
- logs (2)
- 大并发 (2)
- 消息中间件 (2)
- 分布式锁 (3)
- 算法 (4)
- 数字证书原理,公钥私钥 (0)
- 数字证书原理 (1)
- 公钥私钥 (1)
- zookeeper (4)
- Hbase (9)
- Hadoop (2)
- storm (2)
- 通信协议 (1)
- Hive (3)
- git (1)
- JVM (2)
- 大数据相关算法 (1)
- idea (5)
- 将博客搬至CSDN (1)
- 设计模式 (2)
- 表达式 (1)
- 代码审查工具 (0)
- 开源项目 (1)
- PyCharm (0)
- python (6)
- Kubernetes (1)
- swagger (1)
- Maven中mirrors和repository的关系 (0)
- RabbitMQ (3)
- redisson (1)
- k8s (2)
- Mac (1)
最新评论
-
misisipi101:
假设库已经分为32个,那么要扩展到64个,怎样做呢
订单分库分表实践总结以及关键步骤 -
mfkxk298:
简单明了的例子,解决了问题,谢谢啦!
关于ListView中notifyDataSetChanged()刷新数据不更新原因 -
whbwang:
" target="_blank" ...
java web开发 高并发处理 -
suguoqian:
...
java web开发 高并发处理 -
xiangnanyujing:
Dubbox+Redis3.0+Spring+Hibernate+zookeeper实现消息推送核心搭建
资源来自于网络----------------------->
图片见附件
项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现效果如下:
我们这个实例主要讲的就是当点击一个分组的时候,分组里的所有人就默认的全部选中,有了这个功能我们在群发的时候就会给我们省去不少麻烦,这样我们就可以不用一个一个的选中了。那么我们就来看看这个效果是怎么样实现吗。
下面是实现步骤。
1、新建类PhoneListItem,用于表示分组中的每一个手机号码。
Java代码:
2011-4-7 15:07:02 上传
下载附件 (36.65 KB)
2011-4-7 15:07:02 上传
下载附件 (36.65 KB)
- package eoe.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;
- }
- }
2、新建布局文件phone_list_item.xml,用于定义分组下面的每一条手机记录的页面布局。
Java代码:
- <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,用于表示一个分组。代码当中主要就是在一个分组里的内容,我们在用一个boolean值,这个就是说当你选中的时候是真,当你没有选中的时候是假,所以这个很主要,有了boolean值才能知道你是否选中。
Java代码:
- 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,用于定义通讯簿的展现页面。
Java代码:
- <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页面。
Java代码:
- package eoe.ydtf;
- 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;
- }
- }
6、新建类PhoneGroupAdapter,实现BaseExpandableListAdapter。
Java代码:
- package eoe.ydtf.android;
-
- import java.util.List;
- import android.content.Context;
- import android.util.Log;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.ViewGroup;
- import android.widget.BaseExpandableListAdapter;
- import android.widget.Button;
- import android.widget.CheckBox;
- import android.widget.CompoundButton;
- import android.widget.CompoundButton.OnCheckedChangeListener;
- import android.widget.TextView;
-
- public class PhoneGroupAdapter extends BaseExpandableListAdapter {
- class ExpandableListHolder { //定义一个内部类,用于保存listitem的3个子视图引用,2个textview和1个checkbox
- TextView tvName;
- TextView tvPhone;
- CheckBox chkChecked;
- }
- private Context context; //父activity
- private LayoutInflater mChildInflater; //用于加载listitem的布局xml
- private LayoutInflater mGroupInflater; //用于加载group的布局xml
- private List<PhoneGroup> groups; //所有group
- //构造方法:参数c - activity,参数group - 所有group
- public PhoneGroupAdapter(Context c,List<PhoneGroup> groups){
- context=c;
- mChildInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- mGroupInflater = (LayoutInflater) context
- .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- this.groups = groups;
- }
- @Override
- public Object getChild(int arg0, int arg1) {//根据组索引和item索引,取得listitem // TODO Auto-generated method stub
- return groups.get(arg0).children.get(arg1);
- }
- @Override
- public long getChildId(int arg0, int arg1) {//返回item索引
- return arg1;
- }
- @Override
- public int getChildrenCount(int groupPosition) {//根据组索引返回分组的子item数
- return groups.get(groupPosition).children.size();
- }
- @Override
- public Object getGroup(int groupPosition) {//根据组索引返回组
- return groups.get(groupPosition);
- }
- @Override
- public int getGroupCount() {//返回分组数
- return groups.size();
- }
- @Override
- public long getGroupId(int groupPosition) {//返回分组索引
- return groupPosition;
- }
- @Override
- public View getGroupView(int position, boolean isExpanded,
- View view, ViewGroup parent) {//根据组索引渲染"组视图"
- ExpandableListHolder holder = null; //清空临时变量holder
- if (view == null) { //判断view(即view是否已构建好)是否为空
- //若组视图为空,构建组视图。注意flate的使用,R.layout.browser_expandable_list_item代表了
- //已加载到内存的browser_expandable_list_item.xml文件
- view = mGroupInflater.inflate(
- R.layout.phone_list_item, null);
- //下面主要是取得组的各子视图,设置子视图的属性。用tag来保存各子视图的引用
- holder = new ExpandableListHolder();
- //从view中取得textView
- holder.tvName = (TextView) view.findViewById(R.id.phone_name);
- //从view中取得textview
- holder.tvPhone = (TextView) view.findViewById(R.id.phone_number);
- //从view中取得checkbox
- holder.chkChecked = (CheckBox) view
- .findViewById(R.id.phone_check);
- // holder.chkChecked.setEnabled(false);//禁用checkbox
- //把checkbox、textview的引用保存到组视图的tag属性中
- view.setTag(holder);
- } else { //若view不为空,直接从view的tag属性中获得各子视图的引用
- holder = (ExpandableListHolder) view.getTag();
- }
- //对应于组索引的组数据(模型)
- PhoneGroup info = this.groups.get(position);
- if (info != null) {
- //根据模型值设置textview的文本
- holder.tvName.setText(info.title);
- //根据模型值设置checkbox的checked属性
- holder.chkChecked.setChecked(info.getChecked());
- holder.chkChecked.setTag(info);
- holder.chkChecked.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- PhoneGroup group=(PhoneGroup)v.getTag();
- group.setChecked(!group.getChecked());
- notifyDataSetChanged();
- }
- });
- }
- // TODO Auto-generated method stub
- return view;
- }
- //行渲染方法
- @Override
- public View getChildView(int groupPosition, int childPosition,
- boolean isLastChild, View convertView, ViewGroup parent) {
- ExpandableListHolder holder = null; //清空临时变量
- if (convertView == null) { //若行未初始化
- //通过flater初始化行视图
- convertView = mChildInflater.inflate(
- R.layout.phone_list_item, null);
- //并将行视图的3个子视图引用放到tag中
- holder = new ExpandableListHolder();
- holder.tvName = (TextView) convertView
- .findViewById(R.id.phone_name);
- holder.tvPhone = (TextView) convertView.findViewById(R.id.phone_number);
- holder.chkChecked = (CheckBox) convertView
- .findViewById(R.id.phone_check);
- // holder.chkChecked.setEnabled(false);
- convertView.setTag(holder);
- } else { //若行已初始化,直接从tag属性获得子视图的引用
- holder = (ExpandableListHolder) convertView.getTag();
- }
- //获得行数据(模型)
- PhoneListItem info = this.groups.get(groupPosition)
- .children.get(childPosition);
- if (info != null) {
- //根据模型数据,设置行视图的控件值
- holder.tvName.setText(info.name);
- holder.tvPhone.setText(info.phone);
- holder.chkChecked.setChecked(info.checked);
- holder.chkChecked.setTag(info);
- holder.chkChecked.setOnClickListener(new OnClickListener(){
- @Override
- public void onClick(View v) {
- CheckBox check=(CheckBox)v;
- PhoneListItem item=(PhoneListItem)v.getTag();
- item.checked=!item.checked;
- // check.setChecked(!check.isChecked());
- }
- });
- }
- return convertView;
- }
- @Override
- public boolean hasStableIds() {//行是否具有唯一id
- return false;
- }
- @Override
- public boolean isChildSelectable(int groupPosition, int childPosition) {//行是否可选
- return true;
- }
- }
发表评论
-
App开发一些总结
2016-04-05 17:48 2554架构因人而异,不同的架构师大多会有不同的看法;架构也因项目 ... -
java 实现微信搜索附近人功能
2015-03-25 18:07 4855最近给andorid做后台查询数据功能,有一个需求是模仿微信 ... -
重庆APP开发 重庆Android 重庆Ios 爬虫科技 重庆爬虫科技
2014-10-06 12:03 9<!--[if gte mso 9]><x ... -
android 操作文件
2012-08-30 11:15 1018package com.liyong; import jav ... -
android获取短信内容
2012-08-29 23:10 1491自己研究: package com.liyong.data; ... -
Android中的日历操作
2012-08-29 13:01 2979日历的操作方法,这里主要用到了ContentProiver的知 ... -
android获取CMNET、WIFI、CMWAP联网状态
2012-08-29 12:40 5834private static final int CMNET= ... -
android实现手势缩放、移动图片
2012-08-28 19:37 9211新建一个触摸监听器类 package com.liyong. ... -
怎样把信息绘制到图片中并且保存到本地硬盘上
2012-08-27 12:53 1322自己研究吧。。。 //解释一下由于我们不是绘制到手机屏幕上所 ... -
关于ListView中notifyDataSetChanged()刷新数据不更新原因
2012-08-26 21:41 50254使用Listview的时候: 当要动态显示更改后的数据(例如数 ... -
android中获取联系人相关信息
2012-08-26 18:42 1630自己看........... public static ... -
修改Android中strings.xml文件, 动态改变数据
2012-08-24 18:25 10800有些朋友可能会动态的修改Android中strings.xml ... -
android内存泄漏
2012-07-25 21:04 1668尽量不要使用setImageBitmap或setImageRe ... -
将程序安装到SD卡
2012-04-28 22:11 855Android 2.2系统的一大改进就是通过Move to S ... -
一个很好的加密解密字符串
2012-04-28 22:11 1114package net.sf.andhsli.hotspotl ... -
获取手机位置
2012-04-28 22:11 978private double[] getGPS() { Lo ... -
获取手机位置
2012-05-04 09:39 1042private double[] getGPS() { Lo ... -
查看内存存储
2012-05-03 10:05 1046import java.io.File; import a ... -
获取本机电话号码
2012-05-03 10:04 1082private String getMyPhoneNumber ... -
发送彩信
2012-05-03 10:04 967Uri uri = Uri.parse(\"cont ...
相关推荐
BaseExpandableListAdapter使用方法
http://blog.csdn.net/richiezhu/article/details/50906152 BaseExpandableListAdapter
NULL 博文链接:https://xieruilin.iteye.com/blog/726494
Android BaseExpandableListAdapter 教程
使用ExpandableListAdapter实现购物车产品列表,然后通过CheckBox来选择产品。 弹出Layout来选择数量。值得产考!
创建ExpandableListView并使用BaseExpandableListAdapter派生类填充它
主要介绍了Android基于BaseExpandableListAdapter实现的二级列表仿通话记录功能,结合具体实例形式分析了Android实现通话记录功能的布局与功能相关操作技巧,需要的朋友可以参考下
利用ExpandableListView实现多级目录的显示。
非常简单易用高效的BaseAdapter封装库,该库对BaseAdapter、ViewPagerAdapter、BaseExpandableListAdapter等视图适配器进行了最大化封装,使用泛型模式对需要绑定的数据进行解耦,使其在开发过程中使用更加简洁,...
收集的一些关于Android的学习...Android之Adapter用法总结,Android中图片的处理,BaseExpandableListAdapter的使用,反编译android app,详解 Android 的 Activity 组件,需要的朋友可以下载查看(直接双击html文件查看即可)
Android开发模拟QQ扩展列表功能的框架,简单易懂,方便使用,比重写 BaseExpandableListAdapter更方面,更灵活可以自定义listView布局!
BaseExpandableListAdapter的用法等
好友列表的一个小例子,使用BaseExpandableListAdapter,完成的资源的复用,很简单很漂亮的例子
可以向QQ好友列表一样,可以有二级列表,使用的是android控件BaseExpandableListAdapter。
EasyBaseAdapter该库对BaseAdapter、RecyclerView.Adapter、ViewPagerAdapter、BaseExpandableListAdapter等视图适配器进行了最大化封装,使用泛型模式对需要绑定的数据进行解耦,使其在开发过程中使用更加简洁,...
android listview SQLite BaseExpandableListAdapter 图片切割 bitmap
正文 首先是最基础的 ExpandableListView vList = (ExpandableListView) this.findViewById(R.id.list); EListAdapter adapter = new EListAdapter(getApplicationContext(), ...一般情况下, ListView是使用notifyData