分享

Android开发之ExpandableListView 第3页

 Library_for_hj 2015-08-03

改变每个组前面的图标,并且图标样式随着合拢和展开不同,则只需要在res/drawable目录下定义文件:Indicator.xml

  1. <selector xmlns:Android="http://schemas./apk/res/android">    
  2.     <item android:state_expanded="true" android:drawable="@drawable/right" />    
  3.     <item android:drawable="@drawable/down"></item>    
  4. </selector>  

在JAVA文件中添加:

  1. 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为:
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout  
  3. xmlns:android="http://schemas./apk/res/android"  
  4. android:orientation = "horizontal"  
  5. android:layout_width="match_parent"  
  6. android:layout_height="match_parent">  
  7. <ImageView  
  8. android:layout_gravity = "center_vertical"  
  9. android:id = "@+id/imageView01"  
  10. android:layout_width = "70px"  
  11. android:layout_height = "70px"  
  12. android:paddingLeft = "30px"  
  13. android:paddingTop = "2px"  
  14. android:paddingBottom = "5px"  
  15. android:src = "@drawable/ic_launcher"/>  
  16. <TextView  
  17. android:layout_gravity = "center_vertical"  
  18. android:id = "@+id/childTV"  
  19. android:layout_width = "match_parent"  
  20. android:layout_height = "match_parent"  
  21. android:paddingLeft = "30px"  
  22. android:paddingTop = "10px"  
  23. android:paddingBottom = "5px"  
  24. android:textSize = "30sp"/>  
  25. </LinearLayout>  
对于其中的属性不再做详细说明。对于ExpandableListView中的数据,还可以用以下方式定义:
  1. List<Map<String, String>> groups = new ArrayList<Map<String, String>>();  
  2.         Map<String, String> group1 = new HashMap<String, String>();  
  3.         group1.put("group""国家");  
  4.          … …   
  5.         groups.add(group1);  
  6. … …  
  7.         //准备第一个一级列表中的二级列表数据:三个二级列表,分别显示"魏国"、"蜀国"和"吴国"   
  8.         List<Map<String, String>> child1 = new ArrayList<Map<String, String>>();  
  9.         Map<String, String> child1Data1 = new HashMap<String, String>();  
  10.         child1Data1.put("child""魏国");  
  11. … …          
  12. child1.add(child1Data1);  
  13.          … …  
  14.         //准备第二个一级列表中的二级列表数据:八个二级列表,显示"关羽"、"张飞"、"典韦"、"吕布"、"曹操"、"甘宁"、"郭嘉"、"周瑜"   
  15.         List<Map<String, String>> child2 = new ArrayList<Map<String, String>>();  
  16.         Map<String, String> child2Data1 = new HashMap<String, String>();  
  17.         child2Data1.put("child""关羽");  
  18.         … …  
  19.         child2.add(child2Data1);  
  20.         … …  
  21.   
  22.         //准备第三个一级列表中的二级列表数据:五个二级列表,显示 "青龙偃月刀"、"丈八蛇矛枪"、 "青钢剑"、"麒麟弓"、"银月枪"    
  23.         List<Map<String, String>> child3 = new ArrayList<Map<String, String>>();  
  24.         Map<String, String> child3Data1 = new HashMap<String, String>();  
  25.         child3Data1.put("child""青龙偃月刀");  
  26.             … …  
  27.         child3.add(child3Data1);  
  28.         … …  
  29.           
  30.       //用一个list对象保存所有的二级列表数据   
  31.         List<List<Map<String, String>>> childs = new ArrayList<List<Map<String, String>>>();  
  32.         childs.add(child1);  
  33.         childs.add(child2);  
  34.         childs.add(child3);  
  35. 针对上述数据定义方式,修改java文件:  
  36. 例如:在getChildView函数中做如下编写,  
  37.             String text = groups.get(groupPosition).get("group");  
  38.             LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);  
  39.             //获取一级列表布局文件,设置相应元素属性   
  40.             LinearLayout linearLayout = (LinearLayout) layoutInflater.inflate(R.layout.group, null);  
  41.             TextView textView = (TextView)linearLayout.findViewById(R.id.textView01);  
  42.             textView.setText(text);  
  43.             return linearLayout;  

这样就可以将自定义的View写入到child中,当然,这里也可以不用布局文件来定义View,也可以自己用代码实现View。

补充知识:

对于ExpandableListView相应的,也有一个ExpandableListActivity与之对应,对于只需要一个ExpandableListView的Activity,则只需要使用ExpandableListActivity来完成相应的功能就可以了。但是需要注意一点的是:在main.xml页面中添加如下代码:
  1. <ListView android:id="@android:id/list" 或android:id="@id/android:list"  
  2. android:layout_width="fill_parent"  
  3. android:layout_height="wrap_content">  
  4. </ListView>  

这个ID不能随便修改,否则会出现异常:java.lang.RuntimeException:Your content must have a ExpandableListView whose id attribute is'android.R.id.list'.

linux

    本站是提供个人知识管理的网络存储空间,所有内容均由用户发布,不代表本站观点。请注意甄别内容中的联系方式、诱导购买等信息,谨防诈骗。如发现有害或侵权内容,请点击一键举报。
    转藏 分享 献花(0

    0条评论

    发表

    请遵守用户 评论公约