Android ExpandableListView嵌套ListView

  ExpandableListView是一个垂直滚动显示两级列表项的视图,与ListView不同的是,它可以有两层:每一层都能够被独立的展开并显示其子项。这些子项来自于与该视图关联的BaseExpandableListAdapter。

     有时候简单的ExpandableListView不能满足我们的需求,比如下面这样的需求:

    

这就需要在ExpandableListView里面再嵌套一个ListView来实现。首先需要重写一个ListView,代码如下:

[java]  view plain  copy
  1. public class SubListView extends ListView {  
  2.     public SubListView(android.content.Context context,  
  3.             android.util.AttributeSet attrs) {  
  4.         super(context, attrs);  
  5.     }  
  6.   
  7.       
  8.     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {  
  9.         int expandSpec = MeasureSpec.makeMeasureSpec(Integer.MAX_VALUE >> 2,  
  10.                 MeasureSpec.AT_MOST);  
  11.         super.onMeasure(widthMeasureSpec, expandSpec);  
  12.   
  13.     }  
  14.   
  15. }  

主要是重写onMeasure方法,这里将heightMeasureSpec参数设大,否则,嵌套的ListView会显示不全。

然后重写我们的BaseExpandableListAdapter,代码如下:

[java]  view plain  copy
  1. class ExpandAdapter extends BaseExpandableListAdapter {  
  2.         private Context context;  
  3.         private ArrayList<ArrayList<String>> childList;  
  4.         private ArrayList<String> groupList;  
  5.         private LayoutInflater inflater;  
  6.   
  7.         public ExpandAdapter(Context context, ArrayList<String> groupList,  
  8.                 ArrayList<ArrayList<String>> childList) {  
  9.             this.childList = childList;  
  10.             this.groupList = groupList;  
  11.             this.context = context;  
  12.             inflater = LayoutInflater.from(context);  
  13.         }  
  14.   
  15.         @Override  
  16.         public int getGroupCount() {  
  17.             // TODO Auto-generated method stub  
  18.             return groupList.size();  
  19.         }  
  20.   
  21.         @Override  
  22.         public int getChildrenCount(int groupPosition) {  
  23.             // TODO Auto-generated method stub  
  24.             // return childList.get(groupPosition).size();  
  25.             return 1;  
  26.         }  
  27.   
  28.         @Override  
  29.         public Object getGroup(int groupPosition) {  
  30.             // TODO Auto-generated method stub  
  31.             return groupList.get(groupPosition);  
  32.         }  
  33.   
  34.         @Override  
  35.         public Object getChild(int groupPosition, int childPosition) {  
  36.             // TODO Auto-generated method stub  
  37.             return childList.get(groupPosition).get(childPosition);  
  38.         }  
  39.   
  40.         @Override  
  41.         public long getGroupId(int groupPosition) {  
  42.             // TODO Auto-generated method stub  
  43.             return 0;  
  44.         }  
  45.   
  46.         @Override  
  47.         public long getChildId(int groupPosition, int childPosition) {  
  48.             // TODO Auto-generated method stub  
  49.             return 0;  
  50.         }  
  51.   
  52.         @Override  
  53.         public boolean hasStableIds() {  
  54.             // TODO Auto-generated method stub  
  55.             return true;  
  56.         }  
  57.   
  58.         @Override  
  59.         public View getGroupView(int groupPosition, boolean isExpanded,  
  60.                 View convertView, ViewGroup parent) {  
  61.             // TODO Auto-generated method stub  
  62.             View groupView = null;  
  63.             if (convertView == null) {  
  64.                 groupView = newGroupView(parent);  
  65.             } else {  
  66.                 groupView = convertView;  
  67.             }  
  68.             bindGroupView(groupPosition, groupView);  
  69.             return groupView;  
  70.         }  
  71.   
  72.         private View newGroupView(ViewGroup parent) {  
  73.             return inflater.inflate(R.layout.group_item_layout, null);  
  74.         }  
  75.   
  76.         private void bindGroupView(int groupPosition, View groupView) {  
  77.             TextView tv = (TextView) groupView.findViewById(R.id.name_text);  
  78.             tv.setText(groupList.get(groupPosition));  
  79.             ImageView iv = (ImageView) groupView  
  80.                     .findViewById(R.id.image_avatar);  
  81.             iv.setOnClickListener(new OnClickListener() {  
  82.   
  83.                 @Override  
  84.                 public void onClick(View v) {  
  85.                     // TODO Auto-generated method stub  
  86.                     Toast.makeText(context, "Hello!", Toast.LENGTH_SHORT)  
  87.                             .show();  
  88.                 }  
  89.   
  90.             });  
  91.         }  
  92.   
  93.         @Override  
  94.         public View getChildView(int groupPosition, int childPosition,  
  95.   
  96.         boolean isLastChild, View convertView, ViewGroup parent) {  
  97.             // TODO Auto-generated method stub  
  98.             View childView = null;  
  99.             if (convertView == null) {  
  100.                 childView = newChildView(parent, groupPosition);  
  101.             } else {  
  102.                 childView = convertView;  
  103.             }  
  104.             bindChildView(groupPosition, childPosition, childView);  
  105.             return childView;  
  106.         }  
  107.   
  108.         private View newChildView(ViewGroup parent, final int groupPosition) {  
  109.             View v = inflater.inflate(R.layout.child_layout, null);  
  110.             SubListView listView = (SubListView) v.findViewById(R.id.sublistview);  
  111.             View foot = inflater.inflate(R.layout.foot_layout, null);  
  112.             listView.addFooterView(foot);  
  113.             // final SubListAdapter adapter = new  
  114.             // SubListAdapter(treeNodes.get(groupPosition).childs,layoutInflater);  
  115.             final SubListAdapter adapter = new SubListAdapter(  
  116.                     childList.get(groupPosition), inflater);  
  117.             listView.setAdapter(adapter);// 设置菜单Adapter  
  118.             listView.setOnItemClickListener(new OnItemClickListener() {  
  119.   
  120.                 @Override  
  121.                 public void onItemClick(AdapterView<?> parent, View view,  
  122.                         int position, long id) {  
  123.                     // TODO Auto-generated method stub  
  124.                     // if(position ==  
  125.                     // treeNodes.get(groupPosition).childs.size()){//foot的点击事件处理  
  126.                     // treeNodes.get(groupPosition).childs.add("New Add");  
  127.                     // adapter.notifyDataSetChanged();  
  128.                     // }else{  
  129.                     // Toast.makeText(parentContext, "当前选中的是:" + position,  
  130.                     // Toast.LENGTH_SHORT).show();  
  131.                     // }  
  132.                     if (position == childList.get(groupPosition).size()) {// foot的点击事件处理  
  133.                         childList.get(groupPosition).add("New Add");  
  134.                         adapter.notifyDataSetChanged();  
  135.                     } else {  
  136.                         Toast.makeText(ExpandableListViewActivity.this,  
  137.                                 "当前选中的是:" + position, Toast.LENGTH_SHORT)  
  138.                                 .show();  
  139.                     }  
  140.                 }  
  141.   
  142.             });  
  143.             return v;  
  144.         }  
  145.   
  146.         private void bindChildView(int groupPosition, int childPosition,  
  147.                 View groupView) {  
  148. //          TextView tv = (TextView) groupView.findViewById(R.id.name_text);  
  149. //          tv.setText(childList.get(groupPosition).get(childPosition));  
  150.         }  
  151.   
  152.         @Override  
  153.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  154.             // TODO Auto-generated method stub  
  155.             return true;  
  156.         }  
  157.   
  158.     }  

这里需要注意的是在getChildrenCount方法里要返回1,不然就会每个二级View里就会生成很多ListView。加载很多其实就是靠ListView的foot的点击事件来处理的。

这里对ExpandableListView也做了一些其他的处理,一并记录下。第一个是将一级View的指示符号放到里右面并且进行了自定义。放到右面的方法如下:

[java]  view plain  copy
  1. <span style="font-size:18px;">int width = getWindowManager().getDefaultDisplay().getWidth();  
  2.         expandableListView.setIndicatorBounds(width - 100, width - 10);</span>  

自定义指示符的方法是在res/drawable下自定义一个xml命名为indicator.xml,如下:

[java]  view plain  copy
  1. <span style="font-size:18px;"><?xml version="1.0" encoding="UTF-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android">  
  3.  <item android:state_expanded="true" android:drawable="@drawable/add_btn_small" />  
  4.  <item android:state_expanded="false" android:drawable="@drawable/head32_32" />  
  5. </selector></span>  

然后在我们的layout里做以下处理:

[java]  view plain  copy
  1. <span style="font-size:18px;">android:groupIndicator="@drawable/indicator"</span>  

这样就会显示我们自定义的View了,需要注意的一点是这里的图片容易拉伸,最好用.9的图。

第二个问题是默认将ExpandableListView默认打开,这个就比较简单了,

[java]  view plain  copy
  1. private void expandView() {  
  2.         for (int i = 0; i < groupList.size(); i++) {  
  3.             expandableListView.expandGroup(i);  
  4.         }  
  5.     }  


有问题交流。

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值