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

Android 開發:實作可展開收起的圓角列表介面

編輯:關於 Android 程式設計

圓角卡片式的可折疊列表,是很常見的一種資訊呈現手法:標題永遠可見,點一下才展開細節內容。這種效果不需要任何第三方套件,靠 LinearLayout 加上 setVisibility() 就能完成。

一、實作思路

結構很單純:每個項目由「標題列」與「內容區」兩個 LinearLayout 組成,預設內容區設為 gone,點擊標題列時在 VISIBLE 與 GONE 之間切換即可。

切換狀態的計數用了 static int 加奇偶判斷。這個寫法能運作,但有個更直觀的方式:直接讀取目前的 visibility 來決定下一步,不需要额外的計數變數。以下範例採用這種寫法。

二、Activity 程式碼

public class MainActivity extends Activity {

    private LinearLayout one, two, three, four;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        one   = (LinearLayout) findViewById(R.id.one_mention);
        two   = (LinearLayout) findViewById(R.id.two_mention);
        three = (LinearLayout) findViewById(R.id.three_mention);
        four  = (LinearLayout) findViewById(R.id.four_mention);

        // 預設全部收合
        one.setVisibility(View.GONE);
        two.setVisibility(View.GONE);
        three.setVisibility(View.GONE);
        four.setVisibility(View.GONE);

        bindToggle(R.id.oneLayout, one);
        bindToggle(R.id.twolayout, two);
        bindToggle(R.id.threelayout, three);
        bindToggle(R.id.fourlayout, four);
    }

    /**
     * 把「標題列」與「內容區」綁在一起,點擊時切換顯示狀態
     */
    private void bindToggle(int headerId, final LinearLayout content) {
        findViewById(headerId).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (content.getVisibility() == View.VISIBLE) {
                    content.setVisibility(View.GONE);
                } else {
                    content.setVisibility(View.VISIBLE);
                }
            }
        });
    }
}

把重複的四段程式碼抽成 bindToggle() 之後,程式碼量少了三分之二,可讀性也更好。原範例用四個 static int 變數搭配 % 2 判斷,除了冗長之外還有個潛在問題:static 變數的生命週期比 Activity 長,畫面旋轉或 Activity 重建後計數值會與實際 UI 狀態不同步,導致第一次點擊沒有反應。改用 getVisibility() 判斷就沒有這個困擾。

三、版面配置與圓角背景

圓角效果由背景 drawable 負責。建立 res/drawable/rounded_card.xml:

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="https://schemas.android.com/apk/res/android"
    android:shape="rectangle">

    <solid android:color="#FFFFFF" />
    <corners android:radius="8dp" />
    <stroke android:width="1dp" android:color="#DDDDDD" />
</shape>

版面中,外層放一個帶此背景的容器,裡面依序放標題列與內容區:

<LinearLayout
    android:id="@+id/oneLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    android:background="@drawable/rounded_card"
    android:layout_margin="8dp"
    android:clickable="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:padding="12dp"
        android:text="第一項標題" />

    <LinearLayout
        android:id="@+id/one_mention"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="12dp"
        android:visibility="gone">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="這裡放詳細內容" />
    </LinearLayout>
</LinearLayout>

android:clickable="true" 別忘了加。若父容器是 LinearLayout 而沒有這個屬性,點擊事件在某些情況下不會被觸發。

四、幾個可以進一步改善的地方

  • 加轉場動畫:直接切換 visibility 顯得生硬。可以在 res/anim/ 中定義 slide_down 與 slide_up 動畫,在切換時搭配 AnimationUtils.loadAnimation() 呼叫 startAnimation(),體驗會好很多。
  • 手風琴效果:若希望展開一項時自動收起其他項,把每個內容區的狀態集中到一個變數或清單管理,在展開前先遍歷收合其餘項目。
  • 改用 RecyclerView:項目數量多、內容來自資料集合時,硬寫四組 LinearLayout 顯然不可行。改用 RecyclerView 搭配自訂 Adapter,在 onBindViewHolder 中依資料決定是否顯示內容區,才是可維護的做法。
  • 無障礙:可折疊元件建議設定 android:contentDescription 說明目前狀態(已展開/已收合),讓 TalkBack 使用者知道點擊會發生什麼事。
  • Material 元件:若使用 Material Design,CardView 提供更好的陰影與圓角控制,MaterialCardView 還支援 ripple 效果,比自己寫 shape 更省事也更美觀。
  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved