編輯:關於 Android 程式設計
圓角卡片式的可折疊列表,是很常見的一種資訊呈現手法:標題永遠可見,點一下才展開細節內容。這種效果不需要任何第三方套件,靠 LinearLayout 加上 setVisibility() 就能完成。
結構很單純:每個項目由「標題列」與「內容區」兩個 LinearLayout 組成,預設內容區設為 gone,點擊標題列時在 VISIBLE 與 GONE 之間切換即可。
切換狀態的計數用了 static int 加奇偶判斷。這個寫法能運作,但有個更直觀的方式:直接讀取目前的 visibility 來決定下一步,不需要额外的計數變數。以下範例採用這種寫法。
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 而沒有這個屬性,點擊事件在某些情況下不會被觸發。
res/anim/ 中定義 slide_down 與 slide_up 動畫,在切換時搭配 AnimationUtils.loadAnimation() 呼叫 startAnimation(),體驗會好很多。onBindViewHolder 中依資料決定是否顯示內容區,才是可維護的做法。android:contentDescription 說明目前狀態(已展開/已收合),讓 TalkBack 使用者知道點擊會發生什麼事。CardView 提供更好的陰影與圓角控制,MaterialCardView 還支援 ripple 效果,比自己寫 shape 更省事也更美觀。
Android 資源圖片讀取機制:drawable 目錄、密度與縮放規則
為何會有多個 drawable 目錄 Android 專案建立時,res 下常會出現 drawable-ldpi、drawable-mdpi、drawable-hdpi
Android Studio 教程(二):基本設置與運行
上一節介紹了 Studio 的優點與安裝上手,沒想到 Google 更新如此之快,很快又推出了修復 bug 的新版本。今天這篇就帶大家預覽 Studio 的界面與基本功能
Android 網絡編程(十):Retrofit2 請求參數注解全解
前言 上一篇我們掌握了 Retrofit 最基礎的 GET 請求寫法,並對請求參數有了初步認識。本篇會把 Retrofit2 提供的各類請求參數注解系統梳理一遍,包括路徑
Android 中文 TTS 語音合成實作教學
Android 系統原生提供了五種語言的語音合成支援:德文、英文、西班牙文、法文與義大利文,其中並不包含中文。好消息是,Android 允許第三方提供中文 TTS 支援,