andorid ExpandableListView 用法

public class ExpandableListViewActivity extends Activity {
//声明对象
private SimpleExpandableListAdapter adapter=null;
List<Map<String,String>> groups;
List<List<Map<String,String>>> childs;
ExpandableListView expandableListView;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.expandable_list_view);


//为ExpandableListView 准备数据
groups=new ArrayList<Map<String,String>>();
Map<String,String> group1= new HashMap<String, String>();
group1.put("group", "我的家人");

Map<String,String> group2=new HashMap<String,String>();
group2.put("group", "我的朋友");

groups.add(group1);
groups.add(group2);

childs =new ArrayList<List<Map<String,String>>>();
List<Map<String,String>> child1=new ArrayList<Map<String,String>>();
Map<String,String> child1Data1=new HashMap<String, String>();
child1Data1.put("child", "妈妈");
child1.add(child1Data1);
Map<String,String> child1Data2=new HashMap<String, String>();
child1Data2.put("child", "爸爸");
child1.add(child1Data2);

List<Map<String,String>> child2=new ArrayList<Map<String,String>>();
Map<String,String> child1Data3=new HashMap<String, String>();
child1Data3.put("child", "小飞");
child2.add(child1Data3);
Map<String,String> childData4=new HashMap<String, String>();
childData4.put("child", "小武");
child2.add(childData4);
childs.add(child1);
childs.add(child2);

//得到ExpandableListView对象
expandableListView =(ExpandableListView)findViewById(R.id.expandable_1);
//实例化适配器
adapter=new SimpleExpandableListAdapter(this,groups,R.layout.tree_group,
new String[]{"group"},new int[]{R.id.tree_group_textView},
childs,R.layout.tree_child,new String[]{"child"},new int[]{R.id.textView1}
);

//设置适配器
expandableListView.setAdapter(adapter);

//设置监听器
expandableListView.setOnChildClickListener(new OnChildClickListener(){
public boolean onChildClick(ExpandableListView parent, View v,
int groupPosition, int childPosition, long id) {
Log.d("text","group"+groupPosition);
Log.d("text", "child"+childPosition);
return false;
}});
}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值