首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 移动开发 > 移动开发 >

ExpandableListActivity 运用

2012-09-24 
ExpandableListActivity 使用ExpandableListActivity?[功能]sdk 也有关于ExpandableListActivity 代码例子

ExpandableListActivity 使用

ExpandableListActivity

?

[功能]

sdk 也有关于ExpandableListActivity 代码例子 路径如下:

?

//存放 Groop 数据public List<String> group;//存放 Child 数据 以其所属于的Group细分 public List<List<String>> child;

?

?

?

?

2. 定义 class ExpandInfoAdapter extends BaseExpandableListAdapter

public class ExpandInfoAdapter extends BaseExpandableListAdapter {@Overridepublic Object getChild(int arg0, int arg1) {// TODO Auto-generated method stubreturn null;}@Overridepublic long getChildId(int arg0, int arg1) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {// TODO Auto-generated method stubreturn null;}@Overridepublic int getChildrenCount(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic Object getGroup(int arg0) {// TODO Auto-generated method stubreturn null;}@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn 0;}@Overridepublic long getGroupId(int arg0) {// TODO Auto-generated method stubreturn 0;}@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent) {// TODO Auto-generated method stubreturn null;}@Overridepublic boolean hasStableIds() {// TODO Auto-generated method stubreturn false;}@Overridepublic boolean isChildSelectable(int groupPosition, int childPosition) {// TODO Auto-generated method stubreturn false;}}

?

?

?

3. 填充ExpandInfoAdapter 的函数

?

* 完成与 Child 功能相关的函数列表

@Overridepublic Object getChild(int arg0, int arg1) {// TODO Auto-generated method stubreturn child.get(arg0).get(arg1);}@Overridepublic long getChildId(int arg0, int arg1) {// TODO Auto-generated method stubreturn arg1;}@Overridepublic int getChildrenCount(int arg0) {// TODO Auto-generated method stubreturn child.get(arg0).size();}@Overridepublic View getChildView(int arg0, int arg1, boolean arg2, View arg3,ViewGroup arg4) {// TODO Auto-generated method stubreturn getChildViewStub(child.get(arg0).get(arg1).toString());}public TextView getChildViewStub(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(lp); text.setTextSize(20); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(s); return text; }

?

?

?

* 再完成与 Group 有关的函数列表

@Overridepublic int getGroupCount() {// TODO Auto-generated method stubreturn group.size();}@Overridepublic long getGroupId(int arg0) {// TODO Auto-generated method stubreturn arg0;}@Overridepublic View getGroupView(int arg0, boolean arg1, View arg2,ViewGroup arg3) {// TODO Auto-generated method stubreturn getGroupViewStub(getGroup(arg0).toString());}public TextView getGroupViewStub(String s) { // Layout parameters for the ExpandableListView AbsListView.LayoutParams lp = new AbsListView.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 64); TextView text = new TextView(activity); text.setLayoutParams(lp); text.setTextSize(20); text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT); text.setPadding(36, 0, 0, 0); text.setText(s); return text; }

?

?

?

4. BaseExpandableListAdapter 除了 Group Child 外 还有一些别的函数 如:

*

public void onGroupExpanded(int groupPosition){        }                public void onGroupCollapsed(int groupPosition){        }

?

public boolean isChildSelectable(int arg0, int arg1) {// TODO Auto-generated method stubreturn true;}

?

public void addItemByValue(String n,String c1,String c2){ group.add(n); List<String> item = new ArrayList<String>(); item.add(NAME+n); item.add(PHONE+c1); item.add(SEX+c2); child.add(item); }

?

?

本例的数据为:

addItemByValue("Griffin","051782214","man");        addItemByValue("Billy","110","mal");        addItemByValue("Kindy","132","femal");        addItemByValue("Patric","13321234562","femal");

?

?

?

6. emulator 运行截图:

?

* Group 的截图:

?ExpandableListActivity 运用

?

?

* Griffin 选项展开截图:

?ExpandableListActivity 运用

?

?

* Patric 选项展开截图:

ExpandableListActivity 运用

?

?

done!

?

1 楼 dengrui0917 2010-05-25   你能在每个选项中添加CheckBox不,能的话请教我吧,我急用,谢谢,我的邮箱:dengrui0917@126.com(可以的话发我邮箱里) 2 楼 gryphone 2010-05-26   dengrui0917 写道你能在每个选项中添加CheckBox不,能的话请教我吧,我急用,谢谢,我的邮箱:dengrui0917@126.com(可以的话发我邮箱里)
dengrui0917 写道你能在每个选项中添加CheckBox不,能的话请教我吧,我急用,谢谢,我的邮箱:dengrui0917@126.com(可以的话发我邮箱里)
可以啊 你只需定制化方法:getChildView() 即可 3 楼 zhuixinjian 2010-05-26   别陷入apidemo里面带的那个例子..


重写adapter,在item里面加上checkbox,外边定义一个数组,定义哪个被check,哪个没有

每次更新notifydatachange就OK了 4 楼 hellorheaven 2010-09-20   很详细,受教了!

热点排行