ViewPager 在 Android 開發裡出現的頻率相當高,本文用一個最小可行範例把它的用法走一遍。
先在 main.xml 中宣告 ViewPager,並在裡頭嵌入 PagerTitleStrip 作為頁面標題:
<RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"
xmlns:tools="https://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<!-- ViewPager 本體 -->
<android.support.v4.view.ViewPager
android:id="@+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center">
<!-- 為每一頁加上標題 -->
<android.support.v4.view.PagerTitleStrip
android:id="@+id/pagerTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="top">
</android.support.v4.view.PagerTitleStrip>
</android.support.v4.view.ViewPager>
</RelativeLayout>
要實現左右滑屏,需要準備三個頁面布局:tab1、tab2、tab3。內容不必華麗,各放一個控制項即可。
tab1.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
tab2.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
tab3.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ProgressBar
android:id="@+id/progressBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<SeekBar
android:id="@+id/seekBar1"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
package com.example.viewpager;
import java.util.ArrayList;
import java.util.List;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.PagerTitleStrip;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
public class MainActivity extends Activity {
private ViewPager viewPager;
private PagerTitleStrip pagerTitle;
private List<View> list; // 裝載可滑動的布局
private List<String> titleList; // 每一頁的標題
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
viewPager = (ViewPager) this.findViewById(R.id.viewPager);
pagerTitle = (PagerTitleStrip) this.findViewById(R.id.pagerTitle);
// 動態加載布局並轉換成頁面
View view1 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab1, null);
View view2 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab2, null);
View view3 = LayoutInflater.from(MainActivity.this).inflate(R.layout.tab3, null);
list = new ArrayList<View>();
list.add(view1);
list.add(view2);
list.add(view3);
// 為每一頁設定標題
titleList = new ArrayList<String>();
titleList.add("left");
titleList.add("middle");
titleList.add("right");
viewPager.setAdapter(new MyAdapter());
viewPager.setCurrentItem(1); // 預設停在 middle 頁
}
private class MyAdapter extends PagerAdapter {
@Override
public int getCount() {
return list.size();
}
@Override
public Object instantiateItem(View container, int position) {
((ViewPager) container).addView(list.get(position));
return list.get(position);
}
@Override
public void destroyItem(View container, int position, Object object) {
// 滑到別頁時移除
((ViewPager) container).removeView(list.get(position));
}
@Override
public CharSequence getPageTitle(int position) {
return titleList.get(position);
}
@Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == arg1;
}
}
}
幾個關鍵角色:getCount() 決定頁數,instantiateItem() 負責把 View 加入容器並回傳,destroyItem() 在離開頁面時移除,getPageTitle() 供 PagerTitleStrip 取用,而 isViewFromObject() 用來判斷物件與 View 是否為同一個。
這個範例刻意寫得很樸素,重點只在把左右滑屏的骨架搭起來;真實專案裡通常會改用 FragmentPagerAdapter,並把每頁的邏輯拆進各自的 Fragment。