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

Android 基礎入門:ExpandableListView 可折疊列表的基本用法

編輯:關於 Android 程式設計

本節介紹的 Adapter 類控件是 ExpandableListView——可折疊列表。它是 ListView 的子類,在其基礎上把列表項分成「組」,每組再包含多個「子項」,樣子類似 QQ 聯繫人列表。用法與 ListView 相似,只是它的資料需由 ExpandableAdapter 提供。官方 API:ExpandableListView。

一、相關屬性

android:childDivider:各組內子項之間的分隔線
android:childIndicator:顯示於子項旁邊的 Drawable
android:groupIndicator:顯示於組旁邊的 Drawable
android:indicatorLeft/Right/Start/End:組指示器的約束位置
android:childIndicatorLeft/Right/Start/End:子項指示器的約束位置

二、實作 ExpandableAdapter 的三種方式

1. 繼承 BaseExpandableListAdapter 自行實作。
2. 用 SimpleExpandableListAdapter 把兩個 List 包裝成 Adapter。
3. 用 SimpleCursorTreeAdapter 把 Cursor 資料包裝成 Adapter。
本節採用第一種,需重寫相關方法。

三、程式碼範例

先看效果圖:

首先是組與子項的版面。item_exlist_group.xml 是組標題:

<TextView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/tv_group_name"
    android:layout_width="match_parent"
    android:layout_height="56dp"
    android:gravity="center_vertical"
    android:paddingLeft="30dp"
    android:textSize="20sp" />

item_exlist_item.xml 是子項(含圖示與文字):

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="5dp">
    <ImageView
        android:id="@+id/img_icon"
        android:layout_width="48dp"
        android:layout_height="48dp"
        android:focusable="false"
        android:src="@mipmap/iv_lol_icon1" />
    <TextView
        android:id="@+id/tv_name"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginLeft="15dp"
        android:focusable="false"
        android:textSize="18sp" />
</LinearLayout>

核心是自訂的 Adapter,它分成「組」與「子項」兩部分來處理。要特別注意:重寫的 isChildSelectable() 必須回傳 true,否則子項的點擊事件不會觸發。

public class MyBaseExpandableListAdapter extends BaseExpandableListAdapter {
    private ArrayList<Group> gData;
    private ArrayList<ArrayList<Item>> iData;
    private Context mContext;

    public MyBaseExpandableListAdapter(ArrayList<Group> gData,
            ArrayList<ArrayList<Item>> iData, Context mContext) {
        this.gData = gData;
        this.iData = iData;
        this.mContext = mContext;
    }

    @Override public int getGroupCount() { return gData.size(); }
    @Override public int getChildrenCount(int groupPosition) { return iData.get(groupPosition).size(); }
    @Override public Group getGroup(int groupPosition) { return gData.get(groupPosition); }
    @Override public Item getChild(int groupPosition, int childPosition) {
        return iData.get(groupPosition).get(childPosition);
    }
    @Override public long getGroupId(int groupPosition) { return groupPosition; }
    @Override public long getChildId(int groupPosition, int childPosition) { return childPosition; }
    @Override public boolean hasStableIds() { return false; }

    // 組的視圖
    @Override
    public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
        ViewHolderGroup holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_group, parent, false);
            holder = new ViewHolderGroup();
            holder.tv_group_name = convertView.findViewById(R.id.tv_group_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderGroup) convertView.getTag();
        }
        holder.tv_group_name.setText(gData.get(groupPosition).getgName());
        return convertView;
    }

    // 子項的視圖
    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
            View convertView, ViewGroup parent) {
        ViewHolderItem holder;
        if (convertView == null) {
            convertView = LayoutInflater.from(mContext).inflate(R.layout.item_exlist_item, parent, false);
            holder = new ViewHolderItem();
            holder.img_icon = convertView.findViewById(R.id.img_icon);
            holder.tv_name = convertView.findViewById(R.id.tv_name);
            convertView.setTag(holder);
        } else {
            holder = (ViewHolderItem) convertView.getTag();
        }
        holder.img_icon.setImageResource(iData.get(groupPosition).get(childPosition).getiId());
        holder.tv_name.setText(iData.get(groupPosition).get(childPosition).getiName());
        return convertView;
    }

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

    private static class ViewHolderGroup { TextView tv_group_name; }
    private static class ViewHolderItem { ImageView img_icon; TextView tv_name; }
}

主版面放入 ExpandableListView:

<ExpandableListView xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/exlist_lol"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:childDivider="#E02D2F" />

MainActivity 準備好組與子項資料,掛上 Adapter,並為子項設定點擊監聽:

public class MainActivity extends AppCompatActivity {
    private ArrayList<Group> gData = null;
    private ArrayList<ArrayList<Item>> iData = null;
    private ExpandableListView exlist_lol;
    private MyBaseExpandableListAdapter myAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        exlist_lol = findViewById(R.id.exlist_lol);

        gData = new ArrayList<>();
        iData = new ArrayList<>();
        gData.add(new Group("AD"));
        gData.add(new Group("AP"));
        gData.add(new Group("TANK"));

        // ... 為每組填入子項(省略)

        myAdapter = new MyBaseExpandableListAdapter(gData, iData, this);
        exlist_lol.setAdapter(myAdapter);

        exlist_lol.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
            @Override
            public boolean onChildClick(ExpandableListView parent, View v,
                    int groupPosition, int childPosition, long id) {
                Toast.makeText(MainActivity.this,
                    "你點擊了:" + iData.get(groupPosition).get(childPosition).getiName(),
                    Toast.LENGTH_SHORT).show();
                return true;
            }
        });
    }
}

要注意的是,子項資料的儲存方式並不一定要用 ArrayList,可依實際需求調整。以上就是 ExpandableListView 的基本用法,掌握「組 + 子項」的兩層資料結構,就能輕鬆做出類 QQ 聯繫人的折疊列表。

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