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

android 学习笔记(1) ExpandableListActivity 自定义view

2011-05-19 23:45 387 查看
首先,这里默认大家会使用SimpleExpandableListAdapter来创建系统自带的列表。

在此基础上,有时候需要自定义group的样子,或者child的样子,主要通过重写getGroupView和getChildView实现。

public class MyExpandableListAdapter extends SimpleExpandableListAdapter{
public MyExpandableListAdapter(Context context,
List<? extends Map<String, ?>> groupData, int groupLayout,
String[] groupFrom, int[] groupTo,
List<? extends List<? extends Map<String, ?>>> childData,
int childLayout, String[] childFrom, int[] childTo) {
super(context, groupData, groupLayout, groupFrom, groupTo, childData,
childLayout, childFrom, childTo);
// TODO Auto-generated constructor stub
}

@Override
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, 64);
LinearLayout ll =new LinearLayout(MainActivity.this);
ll.setLayoutParams(lp);
TextView tvName = new TextView(MainActivity.this);
tvName.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
tvName.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
tvName.setPadding(50, 0, 0, 0);
tvName.setTextSize(20);
HashMap<String,String> group = (HashMap<String,String>)getGroup(groupPosition);
tvName.setText(group.get("key").toString())
ll.addView(tvImg);
ll.addView(tvName);
return ll;
}

@Override
public View getChildView(int groupPosition, int childPosition,
boolean isLastChild, View convertView, ViewGroup parent) {
//跟上面的道理相同
}


上面的代码就是一个示例,显示的group将是tvName的样子,当然可以添加其他比如图片之类的view。

外带一个消除自带group的箭头的方法

MainActivity.this.getExpandableListView()。setGroupIndicator(null);

这里可以如果传入null则是消除箭头,如果想替换成自己的图标则传入一个drawable即可
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: