android 适配器Adpter的使用总结 之 BaseExpandableListAdapter

android 适配器Adpter的使用总结 之 BaseExpandableListAdapter

2011-06-20 23:37 by 雪夜&流星, 94 visits, 收藏, 编辑

转载请标明出处:http://www.cnblogs.com/tanlon/archive/2011/06/20/2085583.html

通过前面三篇文章:

Android 适配器Adapter的学习:http://www.cnblogs.com/tanlon/archive/2011/05/21/2053009.html

Android 适配器Adpter的使用总结:http://www.cnblogs.com/tanlon/archive/2011/06/20/2084901.html

Android 适配器Adpter的使用总结 之 CursorAdpter:http://www.cnblogs.com/tanlon/archive/2011/06/20/2085562.html

我们对Adpter有了较深的理解之后,我们来了解一下BaseExpandableListAdapter也就是二级树ExpandableListView的数据填充器。

我们应该先了解一下关于BaseExpandableListAdapter中的一些方法的含义和用法:

http://www.cnblogs.com/over140/archive/2011/01/18/1937921.html

如我的上篇博文<Android 适配器Adpter的使用总结 之 CursorAdpter>提到的BaseExpandableListAdapter则是实现了ExpandableListAdpter、HeterogeneousExpandableList接口,同理CursorTreeAdapter继承了BaseExpandableListAdapter,并且衍生出了ResourceCusorTreeAdapter,由ResourceCusorTreeAdapter又衍生出了SimpleCursorTreeAdapter。除此之外还有SimpleExpandableListAdapter也是继承自BaseExpandableListAdapter.

在这里还是以SimpleExpandableListAdapter为例,做一个小demo:

布局文件:

View Code
    
    
<? xml version = " 1.0 " encoding = " utf-8 " ?> < LinearLayout xmlns:android = " http://schemas.android.com/apk/res/android " android:layout_width = " fill_parent " android:layout_height = " fill_parent " > < ExpandableListView android:layout_height = " fill_parent " android:layout_width = " fill_parent " android:id = " @+id/expandableListView " /> </ LinearLayout >
View Code
    
    
<? xml version = " 1.0 " encoding = " utf-8 " ?> < RelativeLayout xmlns:android = " http://schemas.android.com/apk/res/android " android:layout_width = " fill_parent " android:layout_height = " fill_parent " android:gravity = " center " > < TextView android:id = " @+id/expandablelistview_tv " android:layout_width = " fill_parent " android:layout_height = " wrap_content " android:layout_alignParentRight = " true " android:text = " expandablelistview_tv " android:layout_centerVertical = " true " android:paddingLeft = " 50dp " android:textSize = " 24dp " /> </ RelativeLayout >

数据填充器:

View Code
数据填充对象的构造、数据的封装:
    
    
public class ListSimpleExpandableListAdapter extends SimpleExpandableListAdapter{ private Context context; private List < Map < String, String >> groupData = new ArrayList < Map < String, String >> (); // 装载group数据 private List < List < Map < String, String >>> childData = new ArrayList < List < Map < String, String >>> (); // 装载child数据 private int expandedGroupLayout; private int collapsedGroupLayout; private String[] groupFrom; private int [] groupTo; private int childLayout; private int lastChildLayout; private String[] childFrom; private int [] childTo; // 构造器 public ListSimpleExpandableListAdapter(Context context, List < Map < String, String >> groupData, int expandedGroupLayout, int collapsedGroupLayout, String[] groupFrom, int [] groupTo, List < List < Map < String, String >>> childData, int childLayout, int lastChildLayout, String[] childFrom, int [] childTo) { super (context, groupData, expandedGroupLayout, collapsedGroupLayout, groupFrom, groupTo, childData, childLayout, lastChildLayout, childFrom, childTo); // TODO Auto-generated constructor stub this .context = context; this .groupData = groupData; this .childData = childData; this .expandedGroupLayout = expandedGroupLayout; this .collapsedGroupLayout = collapsedGroupLayout; this .groupFrom = groupFrom; this .groupTo = groupTo; this .childLayout = childLayout; this .lastChildLayout = lastChildLayout; this .childFrom = childFrom; this .childTo = childTo; } // 获取group对象 @Override public Object getGroup( int groupPosition) { // TODO Auto-generated method stub return groupData; } // 获取group对象的数量 @Override public int getGroupCount() { // TODO Auto-generated method stub return groupData.size(); } // 获取group的id @Override public long getGroupId( int groupPosition) { // TODO Auto-generated method stub return groupPosition; } @Override public View getGroupView( int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) { // TODO Auto-generated method stub GroupHolder groupHolder; if (convertView == null ){ convertView = LayoutInflater.from(context).inflate(expandedGroupLayout, null ); groupHolder = new GroupHolder(); // groupHolder.imageView=(ImageView) convertView.findViewById(R.id.expandablelistview_iv); groupHolder.textView = (TextView) convertView.findViewById(R.id.expandablelistview_tv); convertView.setTag(groupHolder); } else { groupHolder = (GroupHolder) convertView.getTag(); } // groupHolder.imageView.setImageResource(Integer.parseInt(groupData.get(groupPosition).get(groupFrom[0]).toString())); groupHolder.textView.setText(groupData.get(groupPosition).get(groupFrom[ 1 ]).toString()); return convertView; } // 子节点是否被选中 @Override public boolean isChildSelectable( int groupPosition, int childPosition) { // TODO Auto-generated method stub return true ; } // 获取子节点的数量 @Override public int getChildrenCount( int groupPosition) { // TODO Auto-generated method stub return childData.get(groupPosition).size(); } // 获取子节点对象 @Override public Object getChild( int groupPosition, int childPosition) { // TODO Auto-generated method stub return childData.get(groupPosition).get(childPosition); } // 获取子节点id @Override public long getChildId( int groupPosition, int childPosition) { // TODO Auto-generated method stub return childPosition; } @Override public View getChildView( int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) { // TODO Auto-generated method stub ChildHolder childHolder; if (convertView == null ){ convertView = LayoutInflater.from(context).inflate(childLayout, null ); childHolder = new ChildHolder(); childHolder.imageView = (ImageView) convertView.findViewById(R.id.listitem_pic); childHolder.title = (TextView) convertView.findViewById(R.id.listitem_title); childHolder.content = (TextView) convertView.findViewById(R.id.listitem_content); convertView.setTag(childHolder); } else { childHolder = (ChildHolder) convertView.getTag(); } childHolder.imageView.setImageResource(Integer.parseInt(childData.get(groupPosition).get(childPosition).get(childFrom[ 0 ]).toString())); childHolder.title.setText(childData.get(groupPosition).get(childPosition).get(childFrom[ 1 ]).toString()); childHolder.content.setText(childData.get(groupPosition).get(childPosition).get(childFrom[ 2 ]).toString()); return convertView; } class GroupHolder{ ImageView imageView; TextView textView; } class ChildHolder{ ImageView imageView; TextView title; TextView content; } }
View Code
    
    
public class ListSimpleExpandableList extends Activity{ private static final String ICON = " ICON " ; private static final String NAME = " NAME " ; private static final String IS_EVEN = " IS_EVEN " ; private ExpandableListAdapter mAdapter; private ExpandableListView expandableListView; protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super .onCreate(savedInstanceState); setContentView(R.layout.groupexpandablelist); List < Map < String, String >> groupData = new ArrayList < Map < String, String >> (); List < List < Map < String, String >>> childData = new ArrayList < List < Map < String, String >>> (); for ( int i = 0 ; i < 20 ; i ++ ) { Map < String, String > curGroupMap = new HashMap < String, String > (); groupData.add(curGroupMap); curGroupMap.put(ICON, String.valueOf(R.drawable.icon)); curGroupMap.put(NAME, " Group " + i); curGroupMap.put(IS_EVEN, (i % 2 == 0 ) ? " This group is even " : " This group is odd " ); List < Map < String, String >> children = new ArrayList < Map < String, String >> (); for ( int j = 0 ; j < 15 ; j ++ ) { Map < String, String > curChildMap = new HashMap < String, String > (); curChildMap.put(ICON, String.valueOf(R.drawable.icon)); curChildMap.put(NAME, " Child " + j); curChildMap.put(IS_EVEN, (j % 2 == 0 ) ? " This child is even " : " This child is odd " ); children.add(curChildMap); } childData.add(children); } expandableListView = (ExpandableListView) findViewById(R.id.expandableListView); mAdapter = new ListSimpleExpandableListAdapter( this , groupData, R.layout.groupexpandablelist_item, 0 , new String[] { ICON ,NAME, IS_EVEN }, null , childData, R.layout.listsimple_item, 0 , new String[] { ICON ,NAME, IS_EVEN }, null ); expandableListView.setAdapter(mAdapter); // 传入null则是消除箭头,如果想替换成自己的图标则传入一个drawable即可 // this.expandableListView.setGroupIndicator(null); } }
有什么不对的地方还望大牛多多指教!
相关代码发布在QQ群:Android中高级技术免费培训QQ群(118949422)
很晚了,睡觉去了,明天有机会再写。

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

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值