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

Android 啟動引導頁實作:ViewPager 搭配頁面指示器

編輯:關於 Android 程式設計

前言

現在幾乎所有應用都會有一個引導頁,用來整體介紹產品特色,然後再跳進主介面。本文實作的是帶翻頁效果的引導頁。

概要設計

功能拆成兩塊:翻頁效果與頁面位置指示器。翻頁用系統自帶的 ViewPager 實作;指示器則用一個 LinearLayout 依頁數放入相同數量的 ImageView,再依據頁面滑動動態切換各圖片的資源。

版面配置

<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"
    tools:context=".MainActivity" >

    <android.support.v4.view.ViewPager
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:id="@+id/welcome_pager"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

    <!-- 圖片位置指示器 -->
    <LinearLayout
        android:id="@+id/director"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:orientation="horizontal"
        android:layout_marginBottom="15dip"
        android:layout_alignParentBottom="true" >

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pageindicator_on" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pageindicator_off" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pageindicator_off" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/pageindicator_off" />
    </LinearLayout>

</RelativeLayout>

ViewPager

官方定義是:一個允許使用者透過左右滑動在多頁資料之間切換的版面配置管理器。要用的部分有兩個——Adapter 負責管理顯示內容,OnPageChangeListener 負責處理頁面切換時的行為,我們的指示器狀態切換就是透過它完成的。

Adapter

class pagerAdapter extends FragmentPagerAdapter {

    public pagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int arg0) {
        // 取得要顯示的物件並設定圖片
        WelcomeFragment fm = new WelcomeFragment();
        fm.setImg(imgs.get(arg0));
        return fm;
    }

    @Override
    public int getCount() {
        return imgs.size();
    }
}

其中 imgs 是一個存放圖片資源 id 的清單,WelcomeFragment 則負責顯示單頁內容。必須實作的兩個方法是:getCount() 回傳頁數,getItem() 回傳指定位置的頁面物件。

List<Integer> imgs = null;
// 初始化引導頁圖片陣列
imgs = new ArrayList<Integer>();
imgs.add(R.drawable.help1);
imgs.add(R.drawable.help2);
imgs.add(R.drawable.help3);
imgs.add(R.drawable.help4);

WelcomeFragment

public class WelcomeFragment extends Fragment {

    View view = null;
    int imgId;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        view = inflater.inflate(R.layout.welcome_fragment, null);

        ImageView fragmentVw = (ImageView) view.findViewById(R.id.welcome_Img);
        fragmentVw.setBackgroundResource(imgId);
        return view;
    }

    /**
     * 為該 Fragment 設定要顯示的圖片
     */
    public void setImg(int imgID) {
        imgId = imgID;
    }
}

對應的版面配置:

<FrameLayout xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <ImageView
        android:id="@+id/welcome_Img"
        android:contentDescription="welcome"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</FrameLayout>

OnPageChangeListener:讓指示器跟著動

這個監聽器用來攔截頁面切換事件。實作後,在 onPageSelected 中走訪指示器容器底下的所有 ImageView,把當前頁設為 on 圖示、其餘設為 off。

/**
 * 頁面切換的事件監聽器
 */
class pageChangeListener implements OnPageChangeListener {

    /**
     * 當某一頁被選中時觸發
     */
    @Override
    public void onPageSelected(int arg0) {
        int count = directorLayout.getChildCount();
        // 順序與頁面顯示順序一致的設為 on,其餘設為 off
        for (int i = 0; i < count; i++) {
            ImageView iv = (ImageView) directorLayout.getChildAt(i);
            if (i == arg0) {
                iv.setBackgroundResource(R.drawable.pageindicator_on);
            } else {
                iv.setBackgroundResource(R.drawable.pageindicator_off);
            }
        }
    }

    @Override
    public void onPageScrolled(int arg0, float arg1, int arg2) {
    }

    @Override
    public void onPageScrollStateChanged(int arg0) {
    }
}

別忘了最後一步

版面與元件都準備好之後,記得在 onCreate 裡把三件事接起來:設定 adapter、註冊 listener、指定初始頁。

ViewPager pager = (ViewPager) findViewById(R.id.welcome_pager);
pager.setAdapter(new pagerAdapter(getSupportFragmentManager()));
pager.setOnPageChangeListener(new pageChangeListener());

實作補充

若你的 minSdkVersion 較高,可以直接改用 androidx.viewpager.widget.ViewPager 或更新的 ViewPager2(後者基於 RecyclerView,並內建 PageTransformer 方便做轉場動畫)。另外,引導頁通常會在最後一頁放一個「開始使用」按鈕,並用本文前面介紹過的 SharedPreferences 計數器判斷是否為首次啟動,兩者搭配起來就是完整的引導流程了。

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