Android之ExpandableList扩展用法(基于BaseExpandableListAdapter)

1.简介

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

2.案例

package com.devin;

import java.util.ArrayList;

import android.app.Activity;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.os.Bundle;
import android.view.Gravity;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class PadTestActivity extends Activity {

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
      
        ArrayList<String> groupList = new ArrayList<String>();
        for (int i = 0; i < 3; i++) {
            groupList.add("title");
        }
        
        ArrayList<String> itemList1 = new ArrayList<String>();
        itemList1.add("Item1");
        itemList1.add("Item2");
        ArrayList<String> itemList2 = new ArrayList<String>();
        itemList2.add("Item1");
        itemList2.add("Item21");
        itemList2.add("Item3");
        ArrayList<String> itemList3 = new ArrayList<String>();
        itemList3.add("Item1");
        itemList3.add("Item2");
        itemList3.add("Item3");
        itemList3.add("Item4");
        ArrayList<ArrayList<String>> childList = new ArrayList<ArrayList<String>>();
        childList.add(itemList1);
        childList.add(itemList2);
        childList.add(itemList3);

        ExpandableListView list = new ExpandableListView(this);
        ExpandableListAdapter mAdapter = new MyExpandableListAdapter(groupList, childList);
        list.setAdapter(mAdapter);

        list.setCacheColorHint(0x00000000);
        list.setSelector(new ColorDrawable(Color.TRANSPARENT));
        list.setGroupIndicator(null);
        for (int i = 0; i < mAdapter.getGroupCount(); i++) {
            list.expandGroup(i);
        }

        setContentView(list);
    }

    private class MyExpandableListAdapter extends BaseExpandableListAdapter {
        private ArrayList<String> groupList;
        private ArrayList<ArrayList<String>> childList;

        MyExpandableListAdapter(ArrayList<String> groupList, ArrayList<ArrayList<String>> childList) {
            this.groupList = groupList;
            this.childList = childList;
        }

        public Object getChild(int groupPosition, int childPosition) {
            return childList.get(groupPosition).get(childPosition);
        }

        private int selectedGroupPosition = -1;
        private int selectedChildPosition = -1;

        public void setSelectedPosition(int selectedGroupPosition, int selectedChildPosition) {
            this.selectedGroupPosition = selectedGroupPosition;
            this.selectedChildPosition = selectedChildPosition;
        }

        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }

        public int getChildrenCount(int groupPosition) {
            return childList.get(groupPosition).size();
        }

        public View getChildView(final int groupPosition, final int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
            TextView textView = null;
            if (convertView == null) {
                textView = new TextView(PadTestActivity.this);
                textView.setPadding(32, 10, 0, 10);
                convertView = textView;
            } else {
                textView = (TextView) convertView;
            }

            textView.setText(getChild(groupPosition, childPosition).toString());

            if (groupPosition == selectedGroupPosition) {
                if (childPosition == selectedChildPosition) {
                    textView.setBackgroundColor(0xffb6ddee);
                } else {
                    textView.setBackgroundColor(Color.TRANSPARENT);
                }
            }

            textView.setOnClickListener(new OnClickListener() {
                public void onClick(View v) {
                    setSelectedPosition(groupPosition, childPosition);
                    notifyDataSetChanged();
                }
            });
            return textView;
        }

        public Object getGroup(int groupPosition) {
            return groupList.get(groupPosition);
        }

        public int getGroupCount() {
            return groupList.size();
        }

        public long getGroupId(int groupPosition) {
            return groupPosition;
        }

        public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
            LinearLayout cotain = new LinearLayout(PadTestActivity.this);
            cotain.setPadding(0, 10, 0, 10);
            cotain.setGravity(Gravity.CENTER_VERTICAL);

            ImageView imgIndicator = new ImageView(PadTestActivity.this);
            TextView textView = new TextView(PadTestActivity.this);
            textView.setText(getGroup(groupPosition).toString());
            textView.setPadding(5, 0, 0, 0);

            if (isExpanded) {
                imgIndicator.setBackgroundResource(R.drawable.macro_minus);
            } else {
                imgIndicator.setBackgroundResource(R.drawable.macro_plus);
            }
            cotain.addView(imgIndicator);
            cotain.addView(textView);
            return cotain;
        }

        public boolean hasStableIds() {
            return true;
        }

        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
    }
}

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

 欢迎关注公众号"Devin说",会不定期更新技术知识

posted on 2012-07-18 10:46  Devin Zhang  阅读(17540)  评论(3编辑  收藏  举报