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

ViewSwitcher 用法:兩個視圖之間的帶動畫切換

編輯:關於 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,內部維護一個下標在兩個子視圖間輪換。注意它只支持兩個子視圖,添加第三個沒有意義。

用 XML 聲明並自帶切換動畫

也可以在佈局裡直接聲明,並指定系統預建的切換動畫:

<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 裡都做復位處理。

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