即兴的灵感

思维是一种艺术; 艺术需要灵感。

博客好友

最新评论

Android之可收缩展开列表ExpandableList

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

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

public class mExpandableListAdapter extends BaseExpandableListAdapter {
// 父列表数据
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, int childPosition,
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, boolean isExpanded,
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, int childPosition) {
return true;
}
//获取某一项的View
private TextView getGenericView() {
TextView textView = new TextView(_ExpandableList.this);
return textView;
}
}

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



 
凤凰涅槃/浴火重生/马不停蹄/只争朝夕
     隐姓埋名/低调华丽/简单生活/完美人生

posted on 2010-10-16 10:11 poetguo 阅读(3718) 评论(0)  编辑  收藏 所属分类: Android


只有注册用户登录后才能发表评论。


网站导航: