您的位置:首页 > 移动开发 > Android开发

Android之可收缩展开列表ExpandableList

2012-09-04 23:49 344 查看
在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;

}

}

可以看出,在实现可伸缩列表上,我们需要集中精神把重头戏放在这个适配器上面
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: