Android之ExpandableList擴充用法(基於BaseExpandableListAdapter)

來源:互聯網
上載者:User

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構建一樣),以便最佳化效能和防止記憶體流失。需要的朋友可以自己構建。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.