Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android 手風琴控件 ExpandableListView 完全指南

編輯:Android 開發實例

ExpandableListView 是 Android 內建的「手風琴」列表控件,支援兩層級的資料結構:分組(Group)與子項(Child)。點一下標題展開內容,再點一下收起,非常適合設定頁、分類目錄、通訊錄分組這類需要階層呈現的介面。

一、ExpandableListView 介紹

這是一個垂直滾動的檢視,用來顯示兩個級別的列表項目,資料來源為 ExpandableListAdapter。每個分組可以獨立展開或收合。

重要方法

  • expandGroup(int groupPos):展開指定位置的分組。
  • setSelectedGroup(int groupPosition):選取指定的分組。
  • setSelectedChild(int groupPosition, int childPosition, boolean shouldExpandGroup):選取指定子項,可選擇是否同時展開所屬分組。
  • getPackedPositionGroup(long packedPosition):從打包位置值取出所屬分組。
  • getPackedPositionForChild(int groupPosition, int childPosition):將分組與子項位置打包成單一數值。
  • getPackedPositionType(long packedPosition):判斷該位置是子項還是分組。
  • isGroupExpanded(int groupPosition):判斷分組是否已展開。

在選單事件中解析點擊位置

ExpandableListContextMenuInfo menuInfo =
        (ExpandableListContextMenuInfo) item.getMenuInfo();

String title = ((TextView) menuInfo.targetView).getText().toString();
int type = ExpandableListView.getPackedPositionType(menuInfo.packedPosition);

if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
    int groupPos = ExpandableListView
            .getPackedPositionGroup(menuInfo.packedPosition);
    int childPos = ExpandableListView
            .getPackedPositionChild(menuInfo.packedPosition);
}

二、ExpandableListAdapter

這是一個介面,負責把底層資料連結到 ExpandableListView。實作這個介面之後,就能提供「依分組分類的子項資料」,並負責產生各個子項與分組的檢視。

重要方法

  • getChildId(int groupPosition, int childPosition):取得指定分組中某子項的資料 ID。
  • getChildrenCount(int groupPosition):回傳指定分組下的子項數量。

完整 Adapter 實作

public class MyExpandableListAdapter extends BaseExpandableListAdapter {

    // 範例資料:children[i] 存放 groups[i] 底下的子項字串陣列
    public String[] groups = {
        "我的好友", "新疆同學", "親戚", "同事"
    };

    public String[][] children = {
        { "胡算林", "張俊峰", "王志軍", "二人" },
        { "李秀婷", "蔡喬", "別高", "余音" },
        { "攤派新", "張愛明" },
        { "馬超", "司道光" }
    };

    public Object getChild(int groupPosition, int childPosition) {
        return children[groupPosition][childPosition];
    }

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

    public int getChildrenCount(int groupPosition) {
        return children[groupPosition].length;
    }

    public TextView getGenericView() {
        // ExpandableListView 的版面參數
        AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT, 64);

        TextView textView = new TextView(ExpandableListDemo.this);
        textView.setLayoutParams(lp);
        // 文字垂直置中
        textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        // 設定文字起始內距
        textView.setPadding(36, 0, 0, 0);
        return textView;
    }

    public View getChildView(int groupPosition, int childPosition,
            boolean isLastChild, View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getChild(groupPosition, childPosition).toString());
        return textView;
    }

    public Object getGroup(int groupPosition) {
        return groups[groupPosition];
    }

    public int getGroupCount() {
        return groups.length;
    }

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

    public View getGroupView(int groupPosition, boolean isExpanded,
            View convertView, ViewGroup parent) {
        TextView textView = getGenericView();
        textView.setText(getGroup(groupPosition).toString());
        return textView;
    }

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

    public boolean hasStableIds() {
        return true;
    }
}

三、設計要點

手風琴列表的核心在於「兩層資料的對齊」:getGroupCount() 決定有幾個攤開的標題,getChildrenCount(groupPosition) 決定每個標題底下有幾個子項,而 getChildView 與 getGroupView 分別負責兩種層級的畫面繪製。範例中把清單抽成 getGenericView(),讓分組與子項共用同一套版面參數,是減少重複程式碼的實用小技巧。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved