Android开发:BaseExpandableListAdapter的使用
2010-11-26 13:49 乱世文章 阅读(885) 评论(0) 编辑 收藏 举报项目需要展示一个通讯簿,通讯簿中的手机号码是分组的,要求勾选组时,自动勾选组下的手机号码,实现效果如下:
下面是实现步骤。
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;
}
}