iT邦幫忙

2021 iThome 鐵人賽

DAY 25
0
Mobile Development

android studio 30天初學筆記系列 第 25

Android Studio初學筆記-Day25-ExpandableListVIew(1)

ExpandableListView

這是一個我個人認為稍微進階的列表,因為它多了包覆一層的效果,不過這也是個常出現的元件,雖然稍微複雜了一點,不過整體還是很好理解的,不過它的篇幅有點長我將分成兩篇來介紹,今天先來介紹它介面的配置還有資料的設定。

activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <ExpandableListView
        android:id="@+id/ep1"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:childDivider="#2196F3"/>
</LinearLayout>
  • 首先在activity_main.xml檔中加入ExpandableListView,在ExpandableListView的屬性部分還有許多可以設定,這裡我只用了子類別的分隔線的部分,值的部分直接輸入需要顏色的色碼即可,其他的可以參考官方文件,大部分都是對指標(展開列表旁的箭頭)的細微或客製化調整。

接著可以再res/layout的資料夾中分別新增兩個新的layout,一個負責組的部分,另一個負責子類別的部分。

language_title.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <TextView
        android:id="@+id/title_name"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical"
        android:paddingLeft="30dp"
        android:text="TextView"
        android:textStyle="bold"
        android:textSize="20sp" />

</LinearLayout>

language_content.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/content_name"
        android:layout_width="match_parent"
        android:layout_height="30dp"
        android:gravity="center_vertical|center"
        android:text="TextView"
        android:textSize="20sp" />

</LinearLayout>

在同MainActivity.java的資料夾中新增兩個java檔來負責設定組和子類別的資料,簡單設計如下。

Group.java

public class Group {
    private String name;

    public Group() {
    }
    public Group (String name){
        this.name = name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}

Content.java

public class Content {
    private String name;
    public Content() {
    }
    public Content (String name){
        this.name=name;
    }
    public String getName() {
        return name;
    }
    public void setName(String name) {
        this.name = name;
    }
}
  • 這兩個負責之後關於ExpandableListView內資料的設定,這裡只是簡單設定一個String而已,可以依自己的需求改變。
    今天就先簡單點題一下,完成這些前置作業,接下來的才是重頭戲。雖然這些還只是摸到ExpandableListView的邊,不過接下來的我希望用完整的一個新篇幅接續講解,我們明天見。/images/emoticon/emoticon08.gif

上一篇
Android Studio初學筆記-Day24-FloatButton和SnackBar
下一篇
Android Studio初學筆記-Day26-ExpandableListVIew(2)
系列文
android studio 30天初學筆記30
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言