編輯:中階開發
要在同一個 Activity 內做「多頁」體驗,ViewFlipper 是最省事的元件之一:它把多個子檢視疊在同一個位置,一次只顯示一個,並可搭配動畫做進出效果。這篇先從最基礎的 XML 佈局與按鈕切換開始。
ViewFlipper 標籤內可以放多個子檢視,這裡每個 LinearLayout 就代表「一頁」。切換時整頁換掉,而不是只換其中一個元件。
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<ViewFlipper android:id="@+id/details"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- 第一頁 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:id="@+id/tv_country"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Country" />
<Spinner android:id="@+id/spinner_country"
android:layout_width="200px"
android:layout_height="55px" />
<Button android:id="@+id/Button_next"
android:layout_width="250px"
android:layout_height="55px"
android:textSize="18px"
android:text="Next" />
</LinearLayout>
<!-- 第二頁 -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff">
<TextView android:id="@+id/tv_income"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="18px"
android:text="Income" />
<EditText android:id="@+id/et_income"
android:layout_width="200px"
android:layout_height="55px" />
<Button android:id="@+id/Button_previous"
android:layout_width="250px"
android:layout_height="55px"
android:textSize="18px"
android:text="Previous" />
</LinearLayout>
</ViewFlipper>
</LinearLayout>
這裡唯一需要牢記的規則就是:ViewFlipper 底下每個直屬子節點就是一頁,頁數與順序完全由 XML 中的撰寫順序決定。
public class Activity1 extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// 為 Spinner 填入資料
Spinner spinnerCountries = (Spinner) findViewById(R.id.spinner_country);
ArrayAdapter countryArrayAdapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_dropdown_item,
new String[] { "Canada", "USA" });
spinnerCountries.setAdapter(countryArrayAdapter);
// Next 按鈕:播放進場動畫後切到下一頁
Button buttonNext = (Button) findViewById(R.id.Button_next);
buttonNext.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
vf.setAnimation(AnimationUtils.loadAnimation(
v.getContext(), R.anim.push_left_in));
vf.showNext();
}
});
// Previous 按鈕:切回上一頁
Button buttonPrevious = (Button) findViewById(R.id.Button_previous);
buttonPrevious.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ViewFlipper vf = (ViewFlipper) findViewById(R.id.details);
vf.setAnimation(AnimationUtils.loadAnimation(
v.getContext(), R.anim.push_left_out));
vf.showPrevious();
}
});
}
}
切換的關鍵只有兩個方法:showNext() 前進一頁、showPrevious() 退回一頁。而 setAnimation() 決定切換時播放哪一段動畫,動畫檔放在 res/anim/ 下,SDK 已內建 push_left_in、push_left_out、slide_left、slide_right 等可直接使用。
範例中的 200px、55px 是像素單位,在不同解析度的裝置上會呈現不同的大小,這是早期教學常見的寫法。正式專案應改用 dip(或 dp)與 sp,才能在不同螢幕密度上有一致的視覺比例。
「前進」與「後退」若用同一段動畫,操作起來會有方向錯亂的感覺。實務上通常準備兩組動畫:前進用 push_left_in / push_left_out,後退用 push_right_in / push_right_out。若覺得 push 系列過於強烈,換成 slide_right 往往更自然。
兩者都能切換子檢視,差別在於 ViewSwitcher 只接受兩個子檢視(常用於 View 與它的縮圖、或文字與編輯框的切換),而 ViewFlipper 沒有數量限制,適合多頁情境。若頁數很多且內容較重,也該考慮改用 ViewPager,因為它會在頁面不可見時銷毀離屏檢視,記憶體表現更好。
用按鈕切換雖然直接,但使用者早就習慣了「用滑的」。下一篇要示範兩種手勢做法:一是透過 OnTouchListener 比較按下與放開的 X 座標差,二是用 GestureDetector 判斷 fling 手勢的速度與距離門檻。
HTML 5 本地儲存實戰:讓行動 Web 應用擺脫 4KB 的枷鎖
HTML 5 最實用的新能力之一,就是把「用戶端儲存」這件事真正標準化。過去十幾年,Web 開發者只能把資料硬塞進 4KB 的 Cookie 裡;現在,透過一組再簡單不過
Android 3.0 Honeycomb 底層細節:硬體加速、Renderscript 與平板 UI
Honeycomb:Android 平板時代的起點 Android 3.0,代號 Honeycomb,是 Android 第一個主要針對平板設計的版本,也是首個導入硬體加
Android 本機資料儲存 API 入門:Preference、SQLite 與內部外部儲存
許多 Android 應用都需要保存資料,例如使用者偏好、設定值、好友清單或圖片。若能在裝置本機完成儲存與讀取,就能降低對網路的依賴,也能提升使用體驗。本文整理 Andr
混合式應用程式模型打造 WebSphere Commerce 行動 App(下)
接續上篇,本文說明 iPhone 版本的四個進階能力,並完整介紹 Android 版本的實作方式。 可翻頁的產品詳情畫面 為了讓使用者能左右滑動瀏覽產品,我們自訂了一個