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
原创粉丝点击