ExpandableListView组件学习笔记

来源:互联网 发布:mac邮件设置qq邮箱 编辑:程序博客网 时间:2024/04/30 21:20

         ExpandableListView的用法与普通ListView的用法非常相似,只是ExpandableListView所显示的列表项应该由ExpandableListAdapter提供,ExpandableListAdapter是一个接口。Adapter类似的是,实现ExpandableListAdapter也有如下三种常用方法:

      (1)扩展BaseExpandableListAdapter实现ExpandableListAdapter

      (2)使用SimpleExpandableListAdapter将两个List集合包装成ExpandableListAdapter

      (3)使用SimpleCursorTreeAdapterCursor中的数据包装成SimpleCursorTreeAdapter

        目前只介绍第一种方法,用BaseExpandableListAdapter进行扩展时,其关键在于扩展如下四个方法

        <>getGroupCount();该方法返回包含组列表项的数量。

        <>getGroupView();该方法返回的View对象作为将作为组列表项。

        <>getChildrenCount();该方法返回特定组所包含的子列表项的数量。

        <>getchildView();该方法返回的View对象将作为特定组、特定位置的子列表项。


示例java代码:

public class MainActivity extends Activity{@Overridepublic void onCreate(Bundle savedInstanceState){super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//创建一个BaseExpandableListAdapter对象ExpandableListAdapter adapter = new BaseExpandableListAdapter(){int[] logos = new int[]{R.drawable.p,R.drawable.z,R.drawable.t}; private String[] armTypes = new String[]{ "神族兵种", "虫族兵种", "人族兵种"};private String[][] arms = new String[][]{{ "狂战士", "龙骑士", "黑暗圣堂", "电兵" },{ "小狗", "刺蛇", "飞龙", "自爆飞机" },{ "机枪兵", "护士MM" , "幽灵" }};// 获取指定组位置、指定子列表项处的子列表项数据@Overridepublic Object getChild(int groupPosition, int childPosition){return arms[groupPosition][childPosition];}@Overridepublic long getChildId(int groupPosition, int childPosition){return childPosition;}@Overridepublic int getChildrenCount(int groupPosition){return arms[groupPosition].length;}private TextView getTextView(){AbsListView.LayoutParams lp = new AbsListView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, 64);TextView textView = new TextView(MainActivity.this);textView.setLayoutParams(lp);textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);textView.setPadding(36, 0, 0, 0);textView.setTextSize(20);return textView;}// 该方法决定每个子选项的外观@Overridepublic View getChildView(int groupPosition, int childPosition,boolean isLastChild, View convertView, ViewGroup parent){TextView textView = getTextView();textView.setText(getChild(groupPosition, childPosition).toString());return textView;}// 获取指定组位置处的组数据@Overridepublic Object getGroup(int groupPosition){return armTypes[groupPosition];}@Overridepublic int getGroupCount(){return armTypes.length;}@Overridepublic long getGroupId(int groupPosition){return groupPosition;}// 该方法决定每个组选项的外观@Overridepublic View getGroupView(int groupPosition, boolean isExpanded,View convertView, ViewGroup parent){LinearLayout ll = new LinearLayout(MainActivity.this);ll.setOrientation(0);ImageView logo = new ImageView(MainActivity.this);logo.setImageResource(logos[groupPosition]);ll.addView(logo);TextView textView = getTextView();textView.setText(getGroup(groupPosition).toString());ll.addView(textView);return ll;}@Overridepublic boolean isChildSelectable(int groupPosition,int childPosition){return true;}@Overridepublic boolean hasStableIds(){return true;}};ExpandableListView expandListView = (ExpandableListView) findViewById(R.id.list);expandListView.setAdapter(adapter);}}


显示结果:


0 0