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

ExpandableListView 用法詳解:可展開的分組清單

編輯:Android 開發實例

需要「大分類底下各有細項」的階層式清單時,ExpandableListView 是最直接的選擇。它的資料結構天生就是兩層:一組 Group,每個 Group 底下掛若干 Child。本文從版面定義一路做到 Adapter 實作。

第一步:在版面檔中定義

<LinearLayout
    android:id="@+id/linearLayout"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <ExpandableListView
        android:id="@+id/expandableListView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />
</LinearLayout>

第二步:準備兩層資料結構

用一個 List<String> 存 Group,再用 List<List<String>> 存每個 Group 底下的 Child。

private List<String> groupArray;
private List<List<String>> childArray;

groupArray = new ArrayList<String>();
childArray = new ArrayList<List<String>>();

groupArray.add("第一行");
groupArray.add("第二行");

List<String> tempArray = new ArrayList<String>();
tempArray.add("第一條");
tempArray.add("第二條");
tempArray.add("第三條");

for (int index = 0; index < groupArray.size(); ++index) {
    childArray.add(tempArray);
}

第三步:實作 Adapter

繼承 BaseExpandableListAdapter,實作群組與子項相關的方法。可以把它想成「兩個 Adapter 合體」:一半方法處理 Group,一半處理 Child。

// ExpandableListView 的 Adapter
public class ExpandableAdapter extends BaseExpandableListAdapter {

    Activity activity;

    public ExpandableAdapter(Activity a) {
        activity = a;
    }

    // ===== Child 相關 =====
    public Object getChild(int groupPosition, int childPosition) {
        return childArray.get(groupPosition).get(childPosition);
    }

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

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

    public View getChildView(int groupPosition, int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {
        String string = childArray.get(groupPosition).get(childPosition);
        return getGenericView(string);
    }

    // ===== Group 相關 =====
    public Object getGroup(int groupPosition) {
        return groupArray.get(groupPosition);
    }

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

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

    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String string = groupArray.get(groupPosition);
        return getGenericView(string);
    }

    // 建立 Group / Child 共用的 View
    public TextView getGenericView(String string) {
        AbsListView.LayoutParams layoutParams = new AbsListView.LayoutParams(
                ViewGroup.LayoutParams.FILL_PARENT, 64);
        TextView text = new TextView(activity);
        text.setLayoutParams(layoutParams);
        text.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
        text.setPadding(36, 0, 0, 0);
        text.setText(string);
        return text;
    }

    public boolean hasStableIds() {
        return false;
    }

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

第四步:綁定 Adapter

ExpandableListView expandableListView =
        (ExpandableListView) findViewById(R.id.expandableListView);
expandableListView.setAdapter(new ExpandableAdapter(Main.this));

幾個實務上的改進空間

沒有用 convertView:範例每次都 new 一個 TextView,捲動時會製造大量物件。正確做法是先判斷 convertView == null 才建立,否則重複使用——這是所有 Adapter 類型的共通原則。

預設展開與監聽:若想預先展開某幾組,用 expandGroup(position);想回應展開/收合事件,掛 setOnGroupExpandListener() 與 setOnGroupCollapseListener();子項點擊則用 setOnChildClickListener()。

Group 與 Child 的樣式通常不同:範例用同一個 getGenericView() 處理兩者,實務上 Group 需要較大字級與背景色、Child 需要縮排,應該分成兩個方法各自產生 View,或改用不同的 XML 布局。

現代替代方案:若只是兩層結構,用 RecyclerView 搭配多種 viewType 實作會更靈活,效能也更好;若資料量更大且需要懶載入,可考慮 Expandable RecyclerView 這類第三方函式庫。ExpandableListView 在當前 API 中仍可使用,但已不建議作為新專案的首選。

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