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

ExpandableListView 實戰:自訂群組樣式與四個常見眉角

編輯:關於 Android 開發

前言:這段時間在認真學 Android 並實際做專案,累積了一些對 ExpandableListView 的心得。若文章中有理解錯誤或表達不清的地方,歡迎在留言中指正。

ExpandableListView 是什麼

它是 ListView 的子類,天生支援兩層結構:外層是可展開的群組(Group),內層是群組底下的子項(Child)。點一下群組標題展開,再點一下收合,就是最典型的使用情境。系統內建的樣式相當樸素,實務上幾乎都會自訂 Group 與 Child 的版面配置。

使用步驟

1、在 XML 版面配置中放一個 ExpandableListView
2、在 Activity 或 Fragment 中準備兩個集合,分別存放 Group 與 Child 的資料並完成初始化
3、自訂 Adapter 並繼承 BaseExpandableListAdapter
4、把 Adapter 設定給 ExpandableListView

版面配置:群組樣式

expandlist_group.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="70dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#d1d1d1"
        android:layout_marginTop="15dp">

        <TextView
            android:id="@+id/tv_group_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="30dp"
            android:text="小貓"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:gravity="center_vertical"/>

        <ImageView
            android:id="@+id/iv_arrow"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:background="@drawable/right_arrow"/>
    </RelativeLayout>
</LinearLayout>

版面配置:子項樣式

expandlist_item.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="45dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="45dp"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"
        android:background="#b3b3b3">

        <TextView
            android:id="@+id/tv_child_name"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_marginLeft="30dp"
            android:text="聲音1"
            android:textColor="#ffffff"
            android:textSize="20sp"
            android:gravity="center_vertical"/>

        <ImageView
            android:id="@+id/iv_sound"
            android:layout_width="25dp"
            android:layout_height="25dp"
            android:layout_alignParentRight="true"
            android:layout_centerVertical="true"
            android:layout_marginRight="20dp"
            android:background="@drawable/sound"/>

        <ImageView
            android:id="@+id/iv_divider"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="#ecf0f1"
            android:layout_marginLeft="10dp"
            android:layout_marginRight="10dp"
            android:layout_alignParentTop="true"/>
    </RelativeLayout>
</LinearLayout>

Adapter 實作

MyExpandableAdapter.java

public class MyExpandableAdapter extends BaseExpandableListAdapter {

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

    public MyExpandableAdapter(Context context, List<String> groupArray,
                               List<List<String>> childArray) {
        mContext = context;
        this.groupArray = groupArray;
        this.childArray = childArray;
    }

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

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

    @Override public Object getGroup(int groupPosition) { return groupArray.get(groupPosition); }

    @Override public Object getChild(int groupPosition, int childPosition) {
        return childArray.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 true; }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        View view = convertView;
        GroupHolder holder = null;
        if (view == null) {
            holder = new GroupHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.expandlist_group, null);
            holder.groupName = view.findViewById(R.id.tv_group_name);
            holder.arrow = view.findViewById(R.id.iv_arrow);
            view.setTag(holder);
        } else {
            holder = (GroupHolder) view.getTag();
        }
        // isExpanded 代表該 Group 目前是否已展開
        if (isExpanded) {
            holder.arrow.setBackgroundResource(R.drawable.dowm_arrow);
        } else {
            holder.arrow.setBackgroundResource(R.drawable.right_arrow);
        }
        holder.groupName.setText(groupArray.get(groupPosition));
        return view;
    }

    @Override
    public View getChildView(int groupPosition, int childPosition, boolean isLastChild,
                             View convertView, ViewGroup parent) {
        View view = convertView;
        ChildHolder holder = null;
        if (view == null) {
            holder = new ChildHolder();
            view = LayoutInflater.from(mContext).inflate(R.layout.expandlist_item, null);
            holder.childName = view.findViewById(R.id.tv_child_name);
            holder.sound = view.findViewById(R.id.iv_sound);
            holder.divider = view.findViewById(R.id.iv_divider);
            view.setTag(holder);
        } else {
            holder = (ChildHolder) view.getTag();
        }
        if (childPosition == 0) {
            holder.divider.setVisibility(View.GONE);
        }
        holder.sound.setBackgroundResource(R.drawable.sound);
        holder.childName.setText(childArray.get(groupPosition).get(childPosition));
        return view;
    }

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

    class GroupHolder {
        TextView groupName;
        ImageView arrow;
    }

    class ChildHolder {
        TextView childName;
        ImageView sound;
        ImageView divider;
    }
}

眉角一:把系統預設的箭頭藏起來

如果你發現效果圖裡沒有那支預設箭頭,那是因為它被刻意隱藏了。ExpandableListView 預設會在群組文字左側畫一個指示器,樣式多半與自訂版面不搭:

expandableListView.setGroupIndicator(null);  // 傳入 null 即不顯示預設指示器

若不做這個設定,畫面會像下圖這樣多出一支突兀的箭頭:

眉角二:自己做一個指示器

既然把預設的藏起來,就得自己在 Group 樣式裡畫一個。做法是在 getGroupView 中依 isExpanded 切換圖示:

// 依展開狀態切換箭頭
if (isExpanded) {
    holder.arrow.setBackgroundResource(R.drawable.dowm_arrow);
} else {
    holder.arrow.setBackgroundResource(R.drawable.right_arrow);
}

展開時 isExpanded 為 true,收合時為 false,據此切換即可。

眉角三:預設展開某一個群組

expandableListView.expandGroup(int groupPosition);

只要傳入群組的索引位置,該群組在畫面呈現時就會是展開狀態。

眉角四:item 高度怎麼設都不對

這題讓我卡了很久,翻到的文章也都沒把原因講清楚,最後是靠不斷實測才找出答案。

結論是:高度必須靠巢狀版面才有辦法控制,外層配置一個高度,內層再配置一個高度,而真正決定 item 高度的是內層那個配置。

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="55dp">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="55dp"
        android:background="#d1d1d1"
        android:layout_marginTop="15dp">

① 外層 55dp、內層也設 55dp,就是預期的樣子:

② 外層改成 75dp、內層維持 55dp,高度幾乎沒有變化:

由此可以推斷,真正生效的是內層的高度。有興趣的人可以試著把其中一層移掉,會發現高度就再也設不動了——我實測過,確實如此。

第一次寫文章,希望對同樣卡關的人有幫助。

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