編輯:關於 Android 程式設計
ViewSwitcher 的作用一句話就能說清:在兩個視圖之間切換時播放動畫。
它的兩個子類大家更熟悉——ImageSwitcher 在切換圖片時加動畫,TextSwitcher 在切換文字時加動畫,API Demos 裡都有示例。但不要因此忽略 ViewSwitcher 本身,有些場合它更合適。
視圖切換是 Android 裡的高頻需求:後台加載數據時顯示 loading,數據到來後顯示內容,加載完畢再切回來。用 ViewSwitcher 封裝這套邏輯,代碼可讀性會好很多。
先看 footer 按鈕的佈局:
<Button xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/btn_loadmorecontacts"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="@android:drawable/list_selector_background"
android:clickable="true"
android:minHeight="?android:attr/listPreferredItemHeight"
android:onClick="onClick"
android:text="Load More Items"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />

加載中的視圖:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:minHeight="?android:attr/listPreferredItemHeight">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/progressbar"
android:gravity="center"
android:padding="10dip"
android:text="Loading…"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#FFFFFF" />
</RelativeLayout>

代碼中把兩個視圖依次 addView 進 ViewSwitcher,並作為 footer 掛到 ListView 上:
public class ViewSwitcherExample extends ListActivity implements OnClickListener {
static final String[] ITEMS = {
"List Item 1", "List Item 2", "List Item 3", "List Item 4", "List Item 5",
"List Item 6", "List Item 7", "List Item 8", "List Item 9", "List Item 10"
};
private ViewSwitcher switcher;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
// 代碼方式創建,第一個 addView 的會先顯示
switcher = new ViewSwitcher(this);
switcher.addView(View.inflate(this, R.layout.btn_loadmore, null));
switcher.addView(View.inflate(this, R.layout.loading_footer, null));
getListView().addFooterView(switcher);
setListAdapter(new ArrayAdapter<>(this,
android.R.layout.simple_list_item_1, ITEMS));
}
@Override
public void onClick(View v) {
switcher.showNext(); // 切到加載中
new getMoreItems().execute(); // 後台取數據
}
private class getMoreItems extends AsyncTask<Void, Void, Object> {
@Override
protected Object doInBackground(Void... params) {
try {
Thread.sleep(3000); // 模擬網絡請求
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Object result) {
switcher.showPrevious(); // 切回按鈕
// 刷新 ListView
}
}
}
showNext 與 showPrevious 是 ViewSwitcher 的核心 API,內部維護一個下標在兩個子視圖間輪換。注意它只支持兩個子視圖,添加第三個沒有意義。
也可以在佈局裡直接聲明,並指定系統預建的切換動畫:
<ViewSwitcher xmlns:android="https://schemas.android.com/apk/res/android"
android:id="@+id/profileSwitcher"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ProgressBar
android:id="@+id/progressbar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="@+id/progressbar"
android:gravity="center"
android:text="Loading…" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="Finished!" />
</RelativeLayout>
</ViewSwitcher>
XML 方式的好處是動畫資源可以在佈局階段就確定,設計與代碼分離;代碼方式的好處是可以運行時動態設置動畫(setInAnimation / setOutAnimation)。兩者可以混用:XML 聲明結構,代碼裡按需替換動畫。
一個實踐提醒:ViewSwitcher 的切換不帶任何狀態判斷,加載失敗時必須手動調 showPrevious 復位,否則界面會一直卡在 loading。若使用 AsyncTask,記得在 onPostExecute 與 onCancelled 裡都做復位處理。
Android學習筆記(八):打造友善的進度對話框體驗
為什麼需要進度對話框?不知道你有沒有遇過這種情況:點擊某個功能後,畫面卻卡在那裡一動也不動,讓人搞不清楚程式到底是當機了還是正在忙。這時候,一個貼心的「Please Wa
Material Design 深入實踐:用自定義 Behavior 打造上滑顯示的返回頂部按鈕
效果預覽完整源碼附於文末。為什麼需要一個「回到頂部」按鈕當頁面內容特別長時,使用者滑到最底部後,若想把畫面拉回頂端往往得反覆滑動,體驗相當折磨。Android 不像 iO
Android 異步任務 AsyncTask 源碼深度解析
相信大家对 AsyncTask 機制都不陌生:繼承 AsyncTask 類別並重寫 doInBackground() 於子線程執行耗時操作;onPostExecute()
手機鎖屏密碼隨時間變化:動態密碼不怕被偷看
如今移動支付日益便捷,為了防止他人隨意使用,很多人都會啟用鎖屏功能。但傳統鎖屏多採用固定密碼,被破解的風險相對較高。那麼有沒有辦法把固定密碼改成隨時間變化的動態密碼呢?以