[关闭]
@coder-pig 2015-09-25T03:46:12.000000Z 字数 7424 阅读 2006

Android基础入门教程——2.4.12 ExpandableListView(可折叠列表)的基本使用

Android基础入门教程


本节引言:

本节要讲解的Adapter类控件是ExpandableListView,就是可折叠的列表,它是ListView的子类,
在ListView的基础上它把应用中的列表项分为几组,每组里又可包含多个列表项。至于样子,
类似于QQ联系人列表,他的用法与ListView非常相似,只是ExpandableListVivew显示的列表项
需由ExpandableAdapter提供。 下面我们来学习这个控件的基本使用!
官方API:ExpandableListView


1.相关属性

  • android:childDivider:指定各组内子类表项之间的分隔条,图片不会完全显示,
    分离子列表项的是一条直线
  • android:childIndicator:显示在子列表旁边的Drawable对象,可以是一个图像
  • android:childIndicatorEnd:子列表项指示符的结束约束位置
  • android:childIndicatorLeft:子列表项指示符的左边约束位置
  • android:childIndicatorRight:子列表项指示符的右边约束位置
  • android:childIndicatorStart:子列表项指示符的开始约束位置
  • android:groupIndicator:显示在组列表旁边的Drawable对象,可以是一个图像
  • android:indicatorEnd:组列表项指示器的结束约束位置
  • android:indicatorLeft:组列表项指示器的左边约束位置
  • android:indicatorRight:组列表项指示器的右边约束位置
  • android:indicatorStart:组列表项指示器的开始约束位置

2.实现ExpandableAdapter的三种方式

1. 扩展BaseExpandableListAdpter实现ExpandableAdapter。
2. 使用SimpleExpandableListAdpater将两个List集合包装成ExpandableAdapter
3. 使用simpleCursorTreeAdapter将Cursor中的数据包装成SimpleCuroTreeAdapter
本节示例使用的是第一个,扩展BaseExpandableListAdpter,我们需要重写该类中的相关方法,
下面我们通过一个代码示例来体验下!


3.代码示例

我们来看下实现的效果图

下面我们就来实现上图的这个效果:

核心是重写BaseExpandableListAdpter,其实和之前写的普通的BaseAdapter是类似的,
但是BaseExpandableListAdpter则分成了两部分:组和子列表,具体看代码你就知道了!
另外,有一点要注意的是,重写isChildSelectable()方法需要返回true,不然不会触发
子Item的点击事件!下面我们来写写:

首先是组和子列表的布局:

item_exlist_group.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:padding="5dp">
  7. <TextView
  8. android:id="@+id/tv_group_name"
  9. android:layout_width="match_parent"
  10. android:layout_height="56dp"
  11. android:gravity="center_vertical"
  12. android:paddingLeft="30dp"
  13. android:text="AP"
  14. android:textStyle="bold"
  15. android:textSize="20sp" />
  16. </LinearLayout>

item_exlist_item.xml

  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:orientation="horizontal"
  6. android:padding="5dp"
  7. android:background="#6BBA79">
  8. <ImageView
  9. android:id="@+id/img_icon"
  10. android:layout_width="48dp"
  11. android:layout_height="48dp"
  12. android:src="@mipmap/iv_lol_icon1"
  13. android:focusable="false"/>
  14. <TextView
  15. android:id="@+id/tv_name"
  16. android:layout_width="wrap_content"
  17. android:layout_height="wrap_content"
  18. android:layout_marginLeft="15dp"
  19. android:layout_marginTop="15dp"
  20. android:focusable="false"
  21. android:text="提莫"
  22. android:textSize="18sp" />
  23. </LinearLayout>

然后是自定义的Adapter类:

MyBaseExpandableListAdapter.java

  1. /**
  2. * Created by Jay on 2015/9/25 0025.
  3. */
  4. public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
  5. private ArrayList<Group> gData;
  6. private ArrayList<ArrayList<Item>> iData;
  7. private Context mContext;
  8. public MyBaseExpandableListAdapter(ArrayList<Group> gData,ArrayList<ArrayList<Item>> iData, Context mContext) {
  9. this.gData = gData;
  10. this.iData = iData;
  11. this.mContext = mContext;
  12. }
  13. @Override
  14. public int getGroupCount() {
  15. return gData.size();
  16. }
  17. @Override
  18. public int getChildrenCount(int groupPosition) {
  19. return iData.get(groupPosition).size();
  20. }
  21. @Override
  22. public Group getGroup(int groupPosition) {
  23. return gData.get(groupPosition);
  24. }
  25. @Override
  26. public Item getChild(int groupPosition, int childPosition) {
  27. return iData.get(groupPosition).get(childPosition);
  28. }
  29. @Override
  30. public long getGroupId(int groupPosition) {
  31. return groupPosition;
  32. }
  33. @Override
  34. public long getChildId(int groupPosition, int childPosition) {
  35. return childPosition;
  36. }
  37. @Override
  38. public boolean hasStableIds() {
  39. return false;
  40. }
  41. //取得用于显示给定分组的视图. 这个方法仅返回分组的视图对象
  42. @Override
  43. public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
  44. ViewHolderGroup groupHolder;
  45. if(convertView == null){
  46. convertView = LayoutInflater.from(mContext).inflate(
  47. R.layout.item_exlist_group, parent, false);
  48. groupHolder = new ViewHolderGroup();
  49. groupHolder.tv_group_name = (TextView) convertView.findViewById(R.id.tv_group_name);
  50. convertView.setTag(groupHolder);
  51. }else{
  52. groupHolder = (ViewHolderGroup) convertView.getTag();
  53. }
  54. groupHolder.tv_group_name.setText(gData.get(groupPosition).getgName());
  55. return convertView;
  56. }
  57. //取得显示给定分组给定子位置的数据用的视图
  58. @Override
  59. public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
  60. ViewHolderItem itemHolder;
  61. if(convertView == null){
  62. convertView = LayoutInflater.from(mContext).inflate(
  63. R.layout.item_exlist_item, parent, false);
  64. itemHolder = new ViewHolderItem();
  65. itemHolder.img_icon = (ImageView) convertView.findViewById(R.id.img_icon);
  66. itemHolder.tv_name = (TextView) convertView.findViewById(R.id.tv_name);
  67. convertView.setTag(itemHolder);
  68. }else{
  69. itemHolder = (ViewHolderItem) convertView.getTag();
  70. }
  71. itemHolder.img_icon.setImageResource(iData.get(groupPosition).get(childPosition).getiId());
  72. itemHolder.tv_name.setText(iData.get(groupPosition).get(childPosition).getiName());
  73. return convertView;
  74. }
  75. //设置子列表是否可选中
  76. @Override
  77. public boolean isChildSelectable(int groupPosition, int childPosition) {
  78. return true;
  79. }
  80. private static class ViewHolderGroup{
  81. private TextView tv_group_name;
  82. }
  83. private static class ViewHolderItem{
  84. private ImageView img_icon;
  85. private TextView tv_name;
  86. }
  87. }

PS:存储子列表的数据不一定要用ArrayList>这种,根据自己的需求
定义~

最后是MainActivity的布局以及Java代码:

布局文件:activity_main.xml

  1. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  2. xmlns:tools="http://schemas.android.com/tools"
  3. android:layout_width="match_parent"
  4. android:layout_height="match_parent"
  5. android:padding="5dp"
  6. tools:context=".MainActivity">
  7. <ExpandableListView
  8. android:id="@+id/exlist_lol"
  9. android:layout_width="match_parent"
  10. android:layout_height="match_parent"
  11. android:childDivider="#E02D2F"/>
  12. </RelativeLayout>

MainActivity.java

  1. public class MainActivity extends AppCompatActivity {
  2. private ArrayList<Group> gData = null;
  3. private ArrayList<ArrayList<Item>> iData = null;
  4. private ArrayList<Item> lData = null;
  5. private Context mContext;
  6. private ExpandableListView exlist_lol;
  7. private MyBaseExpandableListAdapter myAdapter = null;
  8. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. super.onCreate(savedInstanceState);
  11. setContentView(R.layout.activity_main);
  12. mContext = MainActivity.this;
  13. exlist_lol = (ExpandableListView) findViewById(R.id.exlist_lol);
  14. //数据准备
  15. gData = new ArrayList<Group>();
  16. iData = new ArrayList<ArrayList<Item>>();
  17. gData.add(new Group("AD"));
  18. gData.add(new Group("AP"));
  19. gData.add(new Group("TANK"));
  20. lData = new ArrayList<Item>();
  21. //AD组
  22. lData.add(new Item(R.mipmap.iv_lol_icon3,"剑圣"));
  23. lData.add(new Item(R.mipmap.iv_lol_icon4,"德莱文"));
  24. lData.add(new Item(R.mipmap.iv_lol_icon13,"男枪"));
  25. lData.add(new Item(R.mipmap.iv_lol_icon14,"韦鲁斯"));
  26. iData.add(lData);
  27. //AP组
  28. lData = new ArrayList<Item>();
  29. lData.add(new Item(R.mipmap.iv_lol_icon1, "提莫"));
  30. lData.add(new Item(R.mipmap.iv_lol_icon7, "安妮"));
  31. lData.add(new Item(R.mipmap.iv_lol_icon8, "天使"));
  32. lData.add(new Item(R.mipmap.iv_lol_icon9, "泽拉斯"));
  33. lData.add(new Item(R.mipmap.iv_lol_icon11, "狐狸"));
  34. iData.add(lData);
  35. //TANK组
  36. lData = new ArrayList<Item>();
  37. lData.add(new Item(R.mipmap.iv_lol_icon2, "诺手"));
  38. lData.add(new Item(R.mipmap.iv_lol_icon5, "德邦"));
  39. lData.add(new Item(R.mipmap.iv_lol_icon6, "奥拉夫"));
  40. lData.add(new Item(R.mipmap.iv_lol_icon10, "龙女"));
  41. lData.add(new Item(R.mipmap.iv_lol_icon12, "狗熊"));
  42. iData.add(lData);
  43. myAdapter = new MyBaseExpandableListAdapter(gData,iData,mContext);
  44. exlist_lol.setAdapter(myAdapter);
  45. //为列表设置点击事件
  46. exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
  47. @Override
  48. public boolean onChildClick(ExpandableListView parent, View v, int groupPosition, int childPosition, long id) {
  49. Toast.makeText(mContext, "你点击了:" + iData.get(groupPosition).get(childPosition).getiName(), Toast.LENGTH_SHORT).show();
  50. return true;
  51. }
  52. });
  53. }
  54. }

4.代码下载:

ExpandableListViewDemo.zip


本节小结:

好的,本节给大家介绍了ExpandableListView的基本使用,嘿嘿,有点意思~
这里只是一个示例,其他的根据自己的需求自行扩展~谢谢

添加新批注
在作者公开此批注前,只有你和作者可见。
回复批注