Android ExpandableList扩展用法

1.简介

  基于基于BaseExpandableListAdapter扩展的ExpandableList用法,现在网上流行的主要有两种:第一种是向BaseExpandableListAdapter传入两个数组,第一个是表示Group(目录头)信息的一维数组,第二个是表示Child(目录子项)的二维数组数组;第二种是构建两个类,一个是表示目录信息的GroupInfo类,另一个是表示子项信息的ChildInfo类,然后传入BaseExpandableListAdapter。通过对比发现,第一种方法由于数组是固定的,而实际项目中往往需要动态变化的目录和子项,因此用处不大,第二种方法文件太多,实现复杂。这里提供一种方法,传递两个个动态的二维数组来实现目录结构.

2.代码

 

[java] view plain copy
 
 在CODE上查看代码片派生到我的代码片
  1. package com.devin;  
  2.   
  3. import java.util.ArrayList;  
  4.   
  5. import android.app.Activity;  
  6. import android.graphics.Color;  
  7. import android.graphics.drawable.ColorDrawable;  
  8. import android.os.Bundle;  
  9. import android.view.Gravity;  
  10. import android.view.View;  
  11. import android.view.View.OnClickListener;  
  12. import android.view.ViewGroup;  
  13. import android.widget.BaseExpandableListAdapter;  
  14. import android.widget.ExpandableListAdapter;  
  15. import android.widget.ExpandableListView;  
  16. import android.widget.ImageView;  
  17. import android.widget.LinearLayout;  
  18. import android.widget.TextView;  
  19.   
  20. public class PadTestActivity extends Activity {  
  21.   
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         super.onCreate(savedInstanceState);  
  24.         
  25.         ArrayList<String> groupList = new ArrayList<String>();  
  26.         for (int i = 0; i < 3; i++) {  
  27.             groupList.add("title");  
  28.         }  
  29.           
  30.         ArrayList<String> itemList1 = new ArrayList<String>();  
  31.         itemList1.add("Item1");  
  32.         itemList1.add("Item2");  
  33.         ArrayList<String> itemList2 = new ArrayList<String>();  
  34.         itemList2.add("Item1");  
  35.         itemList2.add("Item21");  
  36.         itemList2.add("Item3");  
  37.         ArrayList<String> itemList3 = new ArrayList<String>();  
  38.         itemList3.add("Item1");  
  39.         itemList3.add("Item2");  
  40.         itemList3.add("Item3");  
  41.         itemList3.add("Item4");  
  42.         ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();  
  43.         childList.add(itemList1);  
  44.         childList.add(itemList2);  
  45.         childList.add(itemList3);  
  46.   
  47.         ExpandableListView list = new ExpandableListView(this);  
  48.         ExpandableListAdapter mAdapter = new MyExpandableListAdapter(groupList, childList);  
  49.         list.setAdapter(mAdapter);  
  50.   
  51.         list.setCacheColorHint(0x00000000);  
  52.         list.setSelector(new ColorDrawable(Color.TRANSPARENT));  
  53.         list.setGroupIndicator(null);  
  54.         for (int i = 0; i < mAdapter.getGroupCount(); i++) {  
  55.             list.expandGroup(i);  
  56.         }  
  57.   
  58.         setContentView(list);  
  59.     }  
  60.   
  61.     private class MyExpandableListAdapter extends BaseExpandableListAdapter {  
  62.         private ArrayList<String> groupList;  
  63.         private ArrayList<ArrayList<String>> childList;  
  64.   
  65.         MyExpandableListAdapter(ArrayList<String> groupList, ArrayList<ArrayList<String>> childList) {  
  66.             this.groupList = groupList;  
  67.             this.childList = childList;  
  68.         }  
  69.   
  70.         public Object getChild(int groupPosition, int childPosition) {  
  71.             return childList.get(groupPosition).get(childPosition);  
  72.         }  
  73.   
  74.         private int selectedGroupPosition = -1;  
  75.         private int selectedChildPosition = -1;  
  76.   
  77.         public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {  
  78.             this.selectedGroupPosition = selectedGroupPosition;  
  79.             this.selectedChildPosition = selectedChildPosition;  
  80.         }  
  81.   
  82.         public long getChildId(int groupPosition, int childPosition) {  
  83.             return childPosition;  
  84.         }  
  85.   
  86.         public int getChildrenCount(int groupPosition) {  
  87.             return childList.get(groupPosition).size();  
  88.         }  
  89.   
  90.         public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {  
  91.             TextView textView = null;  
  92.             if (convertView == null) {  
  93.                 textView = new TextView(PadTestActivity.this);  
  94.                 textView.setPadding(32, 10, 0, 10);  
  95.                 convertView = textView;  
  96.             } else {  
  97.                 textView = (TextView) convertView;  
  98.             }  
  99.   
  100.             textView.setText(getChild(groupPosition, childPosition).toString());  
  101.   
  102.             if (groupPosition == selectedGroupPosition) {  
  103.                 if (childPosition == selectedChildPosition) {  
  104.                     textView.setBackgroundColor(0xffb6ddee);  
  105.                 } else {  
  106.                     textView.setBackgroundColor(Color.TRANSPARENT);  
  107.                 }  
  108.             }  
  109.   
  110.             textView.setOnClickListener(new OnClickListener() {  
  111.                 public void onClick(View v) {  
  112.                     setSelectedPosition(groupPosition, childPosition);  
  113.                     notifyDataSetChanged();  
  114.                 }  
  115.             });  
  116.             return textView;  
  117.         }  
  118.   
  119.         public Object getGroup(int groupPosition) {  
  120.             return groupList.get(groupPosition);  
  121.         }  
  122.   
  123.         public int getGroupCount() {  
  124.             return groupList.size();  
  125.         }  
  126.   
  127.         public long getGroupId(int groupPosition) {  
  128.             return groupPosition;  
  129.         }  
  130.   
  131.         public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {  
  132.             LinearLayout cotain = new LinearLayout(PadTestActivity.this);  
  133.             cotain.setPadding(0, 10, 0, 10);  
  134.             cotain.setGravity(Gravity.CENTER_VERTICAL);  
  135.   
  136.             ImageView imgIndicator = new ImageView(PadTestActivity.this);  
  137.             TextView textView = new TextView(PadTestActivity.this);  
  138.             textView.setText(getGroup(groupPosition).toString());  
  139.             textView.setPadding(5, 0, 0, 0);  
  140.   
  141.             if (isExpanded) {  
  142.                 imgIndicator.setBackgroundResource(R.drawable.macro_minus);  
  143.             } else {  
  144.                 imgIndicator.setBackgroundResource(R.drawable.macro_plus);  
  145.             }  
  146.             cotain.addView(imgIndicator);  
  147.             cotain.addView(textView);  
  148.             return cotain;  
  149.         }  
  150.   
  151.         public boolean hasStableIds() {  
  152.             return true;  
  153.         }  
  154.   
  155.         public boolean isChildSelectable(int groupPosition, int childPosition) {  
  156.             return true;  
  157.         }  
  158.     }  
  159. }  

3.
上述代码中,过向BaseExpandableListAdapter传递两个动态数组groupList(表示目录头信息)和childList (表示子项信息)来构建目录,一方面能够实现动态的添加数据,另一方面简化了实现,一举两得。另外,重写的BaseExpandableListAdapter,如果应用在实际项目中,需要对getGroupView()和getChildView()方法进行构建缓存(和ListView构建一样),以便优化性能和防止内存泄漏。需要的朋友可以自己构建.

posted @ 2016-12-30 11:38  天涯海角路  阅读(118)  评论(0)    收藏  举报