ExpandableListView组件中CheckBox全选的使用(使用观察者模式)
来源:互联网 发布:如何用js实现图片轮播 编辑:程序博客网 时间:2024/04/29 03:49
ExpandableListView组件类似于手机QQ的好友分组组件,点击分组可展开显示分组下的子数据,很多时候我们会利用ExpandableListView实现子数据选中以及分组钟全部数据选中的功能,本篇博客就是介绍该功能的实现,以及观察者模式的使用。
首先看下实现截图:
然后贴下Activity代码:
package com.example.observer;import java.util.ArrayList;import java.util.List;import android.app.Activity;import android.os.Bundle;import android.view.View;import android.widget.ExpandableListView;import android.widget.ExpandableListView.OnChildClickListener;import android.widget.TextView;import com.example.testdemo.R;/** * 观察者模式实现 ExpandableListView 的全选功能 * @author yhw * */public class ObserverActivity extends Activity implements OnChildClickListener{private ExpandableListView expandableListView;private ExpandAdapter expandAdapter;private List<Group>groups;private TextView textView; //显示选中对象的viewprivate StringBuffer buffer;private List<Object> selecteds=new ArrayList<Object>(); //存放选中对象的集合 @Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_observer);expandableListView=(ExpandableListView) findViewById(R.id.expandablelistview);textView=(TextView)findViewById(R.id.selectedview);expandableListView.setOnChildClickListener(this);buffer=new StringBuffer();showData();}private final static String provinces[]={"陕西省","内蒙古","山西省","辽宁省"};private final static String citys[][]={{"西安市","咸阳市","汉中市","榆林市","延安市"},{"呼和浩特","锡林郭勒","巴彦淖尔","鄂尔多斯"},{"太原市","吕梁市","大同市"},{"沈阳市","大连市"}};private void showData(){groups=new ArrayList<Group>();for (int i = 0; i < provinces.length; i++) {Group group=new Group();group.setGroupName(provinces[i]);group.setSelected(false);List<Child> childs=new ArrayList<Child>();for (int j = 0; j < citys[i].length; j++) {Child child=new Child();child.setSelected(false);child.setChildName(citys[i][j]);childs.add(child);child.addObserver(group);group.addObserver(child);}group.setChilds(childs);groups.add(group);}if(expandAdapter==null){expandAdapter=new ExpandAdapter(this, groups);expandableListView.setAdapter(expandAdapter);}else{expandAdapter.notifyDataSetChanged();}}@Overridepublic boolean onChildClick(ExpandableListView parent, View v,int groupPosition, int childPosition, long id) {Child child=groups.get(groupPosition).getChilds().get(childPosition);child.changeChecked();showSelected();expandAdapter.notifyDataSetChanged();return false;}/**显示选中的数据*/public void showSelected(){//清除selecteds.clear();buffer.delete(0, buffer.length());for (Group group: groups) {if(group.isSelected()){selecteds.add(group);}else{for (Child child : group.getChilds()) {if(child.isSelected()){selecteds.add(child);}}}}if(selecteds.size()>0){for (Object object : selecteds) {if(object instanceof Group){buffer.append(((Group)object).getGroupName()+"[组]、");}if(object instanceof Child){buffer.append(((Child)object).getChildName()+"、");}}textView.setText(buffer.toString());}}}
布局文件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:padding="5dp" android:orientation="vertical" tools:context="com.example.observer.ObserverActivity" > <TextView android:id="@+id/selectedview" android:layout_width="match_parent" android:layout_height="wrap_content" android:textSize="16sp"/> <ExpandableListView android:id="@+id/expandablelistview" android:layout_width="match_parent" android:layout_height="0dp" android:layout_weight="1" android:layout_marginTop="10dp" android:groupIndicator="@null"/></LinearLayout>
最后是两个实体类的代码(两个实体类本身即属于观察者,又同时属于被观察者):
分组实体需要继承Observable类和实现Observer接口
package com.example.observer;import java.util.List;import java.util.Observable;import java.util.Observer;import android.util.Log;public class Group extends Observable implements Observer{private String groupName;private List<Child> childs;private boolean isSelected;public String getGroupName() {return groupName;}public void setGroupName(String groupName) {this.groupName = groupName;}public List<Child> getChilds() {return childs;}public void setChilds(List<Child> childs) {this.childs = childs;}public boolean isSelected() {return isSelected;}public void setSelected(boolean isSelected) {this.isSelected = isSelected;}public void changeChecked(){isSelected=!isSelected;setChanged();notifyObservers(isSelected);}@Overridepublic void update(Observable observable, Object data) {Log.i("YHW", "group update");boolean flag=true;for (Child child: childs) {if(!child.isSelected()){flag=false;}}isSelected=flag;}}
子数据实体需要继承Observable类和实现Observer接口
<span style="color:#333333;">package com.example.observer;import java.util.Observable;import java.util.Observer;import android.util.Log;public class Child extends Observable implements Observer{private String childName;private boolean isSelected;public String getChildName() {return childName;}public void setChildName(String childName) {this.childName = childName;}public boolean isSelected() {return isSelected;}public void setSelected(boolean isSelected) {this.isSelected = isSelected;}public void changeChecked(){isSelected=!isSelected;setChanged();notifyObservers();}@Overridepublic void update(Observable observable, Object data) {Log.i("YHW", "child update");if(data instanceof Boolean){isSelected=(Boolean) data;}}}</span><span style="color:#ff0000;"></span>
总结:
总体实现比较简单,就不做过多阐述了,主要就是通过观察者模式,实现分组数据和子数据之间互相监听选中与否
3 0
- ExpandableListView组件中CheckBox全选的使用(使用观察者模式)
- ExpandableListView组件的使用
- (ExpandableListView)android中树形组件的使用
- (checkbox)的使用 (全选/取消全选)示例
- ExpandableListView+CheckBox使用注意事项
- 解决TreeView中使用JavaScript完成CheckBox全选的办法
- jQuery实现Checkbox中项目开发全选全不选的使用
- Android中可展开的列表组件(ExpandableListView)的使用
- 安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
- 安卓ListView中CheckBox的使用(支持Item列表项的删除,全选,全不选)
- 组件使用之ExpandableListView
- 最精简的全选CheckBox(使用getElementById)
- HTML select ,radio的使用,checkbox全选
- Android全选删除CheckBox的简单使用
- ExpandableListView 跟 CheckBox 组合使用!!
- ExpandableListView 和CheckBox结合使用
- Java中观察者模式的使用(二)
- Java 中观察者模式的使用
- HttpClient 4.0的使用工具类
- vector学习小节(二)
- 如何在Mac OS X上安装 Ruby运行环境
- Redis学习笔记(四) Spring与Jedis的集成
- C++学习之Pair
- ExpandableListView组件中CheckBox全选的使用(使用观察者模式)
- QC ALM11启动
- windows版mongodb不知道安装在哪儿
- ThinkPHP 方法函数总结
- 树莓派中文输入法设置
- Objective-c的@property 详解
- tslib库的安装及各种错误处理
- if语句的使用
- 快速排序