Android之可收缩展开列表ExpandableList

在Android的app包中,有这么一个类,这个类继承自Activity,它叫ExpandableListActivity。顾名思义,从它的名字可以看出该类是一种可扩展性的列表List,我们这里理解成可伸缩的列表,也就是通过继承ExpandableListActivity可以实现列表的可展开/收缩的功能。

本文我们主要介绍这种列表的显示是如何实现的,在ListActivity的使用中,我们知道一旦继承了ListActivity,该类就意味这具备了List的功能,同样的,我们将一个类继承了ExpandableListActivity,就可以直接调用该类本身的ExpandableList对象,并直接赋予一个自定义的适配器setListAdapter(adapter);,因此,整个实现的重心放在如何设计这个适配器上面,以下是适配器的一个举例。

public class mExpandableListAdapter extendsBaseExpandableListAdapter {
// 父列表数据
private String[] groups =
{
“随时随地”,
“即兴时代”,
“ATAAW.COM”,
};
// 子列表数据
private String[][] children =
{
{ “即兴” },
{ “随时随地”, “即兴时代” },
{ “随时随地”, “即兴时代”, “ATAAW.COM” },
};
@Override
public Object getChild(int groupPosition, int childPosition){
return children[groupPosition][childPosition];
}

@Override
public long getChildId(int groupPosition, int childPosition){
return childPosition;
}

@Override
public int getChildrenCount(int groupPosition) {
return children[groupPosition].length;
}

// 取子列表中的某一项的 View
@Override
public View getChildView(int groupPosition, intchildPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getChild(groupPosition,childPosition).toString());
return textView;
}

@Override
public Object getGroup(int groupPosition) {
return groups[groupPosition];
}

@Override
public int getGroupCount() {
return groups.length;
}

@Override
public long getGroupId(int groupPosition) {
return groupPosition;
}

//父列表中的某一项的View
@Override
public View getGroupView(int groupPosition, booleanisExpanded,
View convertView, ViewGroup parent) {
TextView textView = getGenericView();
textView.setText(getGroup(groupPosition).toString());
return textView;
}

@Override
public boolean hasStableIds() {
return true;
}

@Override
public boolean isChildSelectable(int groupPosition, intchildPosition) {
return true;
}
//获取某一项的View
private TextView getGenericView() {
TextView textView = new TextView(_ExpandableList.this);
return textView;
}
}

可以看出,在实现可伸缩列表上,我们需要集中精神把重头戏放在这个适配器上面


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值