Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android 開發實戰:ExpandableListView 可展開列表完整教學

編輯:關於 Android 程式設計

在很多應用場景裡,單純的 ListView 無法滿足需求。當我們需要以「多組清單」的形式呈現資料時,就該請出 ExpandableListView 這個可展開的列表控制項了。它的作用就像把 ListView 進行分組——好比我們使用 QQ 時的「我的好友」「陌生人」「黑名單」,點一下展開,再點一下又收合回去。

基本觀念

ExpandableListView 是一個可垂直滾動、顯示兩級列表項的視圖。與 ListView 不同,它具有兩層結構,每一層都能被獨立展開並顯示其子項,而這些子項來自與該視圖關聯的 ExpandableListAdapter。

每個可展開的列表項旁都有一個指示符(箭頭),用以說明目前狀態(已展開、未展開、子項、最後一個子項等)。可以透過 setChildIndicator(Drawable) 與 setGroupIndicator(Drawable)(或對應的 XML 屬性)來自訂樣式,當然也可以沿用系統預設樣式。

Adapter 的關鍵方法

和 ListView 一樣,ExpandableListView 也需要 Adapter 作為資料橋梁。適用的 Adapter 通常會繼承 BaseExpandableListAdapter,並重點覆寫 getGroupView 與 getChildView 這兩個方法。以下是幾個最常被使用的抽象方法:

[java]
// 取得指定分組、指定子項關聯的資料
public abstract Object getChild(int groupPosition, int childPosition);
// 取得子項的 View
public abstract View getChildView(int groupPosition, int childPosition,
   boolean isLastChild, View convertView, ViewGroup parent);
// 取得指定分組的子元素數量
public abstract int getChildrenCount(int groupPosition);
// 取得與指定分組關聯的資料
public abstract Object getGroup(int groupPosition);
// 取得分組總數
public abstract int getGroupCount();
// 取得分組的 View
public abstract View getGroupView(int groupPosition, boolean isExpanded,
   View convertView, ViewGroup parent);

其中 getGroupView 只負責回傳分組的視圖,若想取得子元素的視圖,則需呼叫 getChildView;而 convertView 參數可用來重用舊的視圖物件,是提升效能的重要機制。

佈局與程式碼實作

首先在 LinearLayout 中放入一個 ExpandableListView:

[xml]
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
   android:layout_width="fill_parent"
   android:layout_height="fill_parent"
   android:orientation="vertical">
   <ExpandableListView
       android:id="@+id/myExpandableListView"
       android:layout_width="fill_parent"
       android:layout_height="fill_parent"
       android:layout_weight="1" />
</LinearLayout>

接著在 Activity 中準備分組資料與子項資料,並套用自訂的 Adapter:

[java]
private List<String> GroupData;// 定義組資料
private List<List<String>> ChildrenData;// 定義組中的子資料

private void LoadListDate() {
   GroupData = new ArrayList<String>();
   GroupData.add("國家");
   GroupData.add("人物");
   GroupData.add("武器");
   ChildrenData = new ArrayList<List<String>>();
   List<String> Child1 = new ArrayList<String>();
   Child1.add("蜀國"); Child1.add("魏國"); Child1.add("吳國");
   ChildrenData.add(Child1);
   // ……其餘分組依此類推
}

@Override
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);
   LoadListDate();
   ExpandableListView myExpandableListView =
       (ExpandableListView) findViewById(R.id.myExpandableListView);
   myExpandableListView.setAdapter(new ExpandableAdapter());
}

private class ExpandableAdapter extends BaseExpandableListAdapter {
   @Override
   public Object getChild(int groupPosition, int childPosition) {
       return ChildrenData.get(groupPosition).get(childPosition);
   }
   @Override
   public View getChildView(int groupPosition, int childPosition,
       boolean isLastChild, View convertView, ViewGroup parent) {
       TextView myText = null;
       if (convertView != null) {
           myText = (TextView) convertView;
           myText.setText(ChildrenData.get(groupPosition).get(childPosition));
       } else {
           myText = createView(ChildrenData.get(groupPosition).get(childPosition));
       }
       return myText;
   }
   @Override
   public int getChildrenCount(int groupPosition) {
       return ChildrenData.get(groupPosition).size();
   }
   @Override
   public Object getGroup(int groupPosition) { return GroupData.get(groupPosition); }
   @Override
   public int getGroupCount() { return GroupData.size(); }
   @Override
   public View getGroupView(int groupPosition, boolean isExpanded,
       View convertView, ViewGroup parent) {
       TextView myText = null;
       if (convertView != null) {
           myText = (TextView) convertView;
           myText.setText(GroupData.get(groupPosition));
       } else {
           myText = createView(GroupData.get(groupPosition));
       }
       return myText;
   }
   @Override
   public boolean hasStableIds() { return false; }
   @Override
   public boolean isChildSelectable(int groupPosition, int childPosition) { return false; }
   private TextView createView(String content) {
       AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
           ViewGroup.LayoutParams.FILL_PARENT, 80);
       TextView myText = new TextView(ExpandableListViewActivity.this);
       myText.setLayoutParams(layoutParams);
       myText.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
       myText.setPadding(80, 0, 0, 0);
       myText.setText(content);
       return myText;
   }
}

進階美化技巧

實務開發中常有各式客製需求,例如為每個 child 或 group 加上圖標、變換背景、預設展開某一組等。以下列幾個常用做法:

設定背景並預設展開第 n 組(n 從 0 起算):

[java]
myExpandableListView.setBackgroundResource(R.drawable.background);
myExpandableListView.expandGroup(0);

讓分組圖標隨展開/收合而改變,可在 res/drawable 下定義 Indicator.xml:

[xml]
<selector xmlns:android="https://schemas.android.com/apk/res/android">
   <item android:state_expanded="true" android:drawable="@drawable/right" />
   <item android:drawable="@drawable/down"></item>
</selector>

再於程式碼中套用:

[java]
myExpandableListView.setGroupIndicator(this.getResources().getDrawable(R.drawable.indicator));

其他常用屬性還包括 android:childDivider(分隔子項的直線)、android:childIndicator(子項指示符)、android:groupIndicator(分組指示符)等。此外,也可以自訂 group.xml 或 child.xml 佈局檔,用 ImageView 搭配 TextView 打造更豐富的視覺呈現。

重要提醒

在 XML 佈局中,若 ExpandableListView 的上層視圖未嚴格定義大小,就不可對其 android:layout_height 使用 wrap_content。例如上層是 ScrollView 時便不建議這麼做,否則會在 setContentView 處拋出空指標錯誤。唯有當上層視圖具有明確尺寸(例如 100 像素)時,才能安全地使用 wrap_content。

最後補充一點:若某個 Activity 只需要一個 ExpandableListView,也可以直接繼承 ExpandableListActivity 來簡化開發。

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