改变每个组前面的图标,并且图标样式随着合拢和展开不同,则只需要在res/drawable目录下定义文件:Indicator.xml
- <selector xmlns:Android="http://schemas./apk/res/android">
- <item android:state_expanded="true" android:drawable="@drawable/right" />
- <item android:drawable="@drawable/down"></item>
- </selector>
在JAVA文件中添加:
- myExpandableListView.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));
效果如下:
对于其他的属性设置,可以参考以下属性说明:
android:childDivider
来分离子列表项的图片或者是颜色。注:图片不会完全显示,分离子列表项的是一条直线
android:childIndicator
在子列表项旁边显示的指示符。注:可以是一个图片
android:childIndicatorLeft
子列表项指示符的左边约束位置。注:即从左端0位置开始计数,比如,假设指示符是一个图标,给定这个属性值为3dip,则表示从左端起3dip开始显示此图标。
android:childIndicatorRight
子列表项指示符的右边约束位置。注:表示右端到什么位置结束
android:groupIndicator
在组列表项旁边显示的指示符。注:可以是一个图片。
android:indicatorLeft
组列表项指示器的左边约束位置。注:表示左端从什么位置开始。
android:indicatorRight
组列表项指示器的右边约束位置。注:表示右端到什么位置结束。
当然,还可以使用自定义的View去描述group和child,自定义的View可以和布局文件一样,写在layout文件夹下。例如命名为group.xml或者child.xml。 例如,我们定义一个child项由一个ImageView和一个TextView来组成,则可以定义child.xml为:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas./apk/res/android"
- android:orientation = "horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <ImageView
- android:layout_gravity = "center_vertical"
- android:id = "@+id/imageView01"
- android:layout_width = "70px"
- android:layout_height = "70px"
- android:paddingLeft = "30px"
- android:paddingTop = "2px"
- android:paddingBottom = "5px"
- android:src = "@drawable/ic_launcher"/>
- <TextView
- android:layout_gravity = "center_vertical"
- android:id = "@+id/childTV"
- android:layout_width = "match_parent"
- android:layout_height = "match_parent"
- android:paddingLeft = "30px"
- android:paddingTop = "10px"
- android:paddingBottom = "5px"
- android:textSize = "30sp"/>
- </LinearLayout>
对于其中的属性不再做详细说明。对于ExpandableListView中的数据,还可以用以下方式定义:
- List<Map<String, String>> groups = new ArrayList<Map<String, String>>();
- Map<String, String> group1 = new HashMap<String, String>();
- group1.put("group", "国家");
- … …
- groups.add(group1);
- … …
- //准备第一个一级列表中的二级列表数据:三个二级列表,分别显示"魏国"、"蜀国"和"吴国"
- List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();
- Map<String, String> child1Data1 = new HashMap<String, String>();
- child1Data1.put("child", "魏国");
- … …
- child1.add(child1Data1);
- … …
- //准备第二个一级列表中的二级列表数据:八个二级列表,显示"关羽"、"张飞"、"典韦"、"吕布"、"曹操"、"甘宁"、"郭嘉"、"周瑜"
- List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();
- Map<String, String> child2Data1 = new HashMap<String, String>();
- child2Data1.put("child", "关羽");
- … …
- child2.add(child2Data1);
- … …
-
- //准备第三个一级列表中的二级列表数据:五个二级列表,显示 "青龙偃月刀"、"丈八蛇矛枪"、 "青钢剑"、"麒麟弓"、"银月枪"
- List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();
- Map<String, String> child3Data1 = new HashMap<String, String>();
- child3Data1.put("child", "青龙偃月刀");
- … …
- child3.add(child3Data1);
- … …
-
- //用一个list对象保存所有的二级列表数据
- List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();
- childs.add(child1);
- childs.add(child2);
- childs.add(child3);
- 针对上述数据定义方式,修改java文件:
- 例如:在getChildView函数中做如下编写,
- String text = groups.get(groupPosition).get("group");
- LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
- //获取一级列表布局文件,设置相应元素属性
- LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);
- TextView textView = (TextView)linearLayout.findViewById(R.id.textView01);
- textView.setText(text);
- return linearLayout;
这样就可以将自定义的View写入到child中,当然,这里也可以不用布局文件来定义View,也可以自己用代码实现View。
补充知识: 对于ExpandableListView相应的,也有一个ExpandableListActivity与之对应,对于只需要一个ExpandableListView的Activity,则只需要使用ExpandableListActivity来完成相应的功能就可以了。但是需要注意一点的是:在main.xml页面中添加如下代码:
- <ListView android:id="@android:id/list" 或android:id="@id/android:list"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content">
- </ListView>
这个ID不能随便修改,否则会出现异常:java.lang.RuntimeException:Your content must have a ExpandableListView whose id attribute is'android.R.id.list'.
|