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

自製 ScrollLayout 的佈局與使用方式(下)

編輯:關於 Android 開發

元件寫完,接著看怎麼用。這一階段只需一個極簡的 Activity 與一份三頁的 XML 版面。

Activity:ScrollViewDemoActivity.java

package com.teleca.robin;

import android.app.Activity;
import android.os.Bundle;

public class ScrollViewDemoActivity extends Activity {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main_scroll);
    }
}

就這樣——所有互動邏輯都在 ScrollLayout 裡,Activity 不必寫任何一行手勢相關的程式碼。這正是自訂 ViewGroup 的價值:把複雜度關進元件裡。

版面:main_scroll.xml

<?xml version="1.0" encoding="utf-8"?>
<com.teleca.robin.ScrollLayout
    xmlns:android="https://schemas.android.com/apk/res/android"
    android:id="@+id/ScrollLayoutTest"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

    <LinearLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/girl">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="第一頁"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/girl2">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="第二頁"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

    <LinearLayout
        xmlns:android="https://schemas.android.com/apk/res/android"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent"
        android:orientation="vertical"
        android:background="@drawable/girl3">

        <TextView
            android:id="@+id/textView1"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="第三頁"
            android:textAppearance="?android:attr/textAppearanceLarge" />
    </LinearLayout>

</com.teleca.robin.ScrollLayout>

幾點實作上的提醒

範例中三頁各放了一個 TextView,其中 android:id="@+id/textView1" 重複出現三次。XML 允許這樣寫,但 findViewById 只會找到第一個——這裡是刻意簡化,實作時請給每頁不同的 id,否則想更新第二頁文字時會找不到人。

其次,ScrollLayout 的 onMeasure 強制要求 EXACTLY 模式,所以它的直接子元件必須明確填滿父容器(fill_parent/match_parent),不要包成 wrap_content,否則會直接丟出 IllegalStateException。

若要在程式碼中主動切頁,呼叫 snapToScreen(1)(帶動畫)或 setToScreen(1)(瞬間跳轉)即可,也能用 getCurScreen() 取得目前頁碼,適合搭配下方頁次指示點使用。

今天還需要自己刻嗎?

把這個系列當成「理解原理」的練習很有價值——你會真正搞懂 onMeasure/onLayout 的職責、Scroller 動畫的驅動方式、以及事件攔截的優先順序。但在真實專案裡,這些問題早已有現成答案:ViewPager(或 ViewPager2 搭配 RecyclerView)提供了 paging 捲動、頁面快取、深連結與無障礙支援,Navigation Component 也與之整合良好。除非有特殊的版面需求,否則請把力氣花在產品本身,而不是重造一個功能較少的輪子。

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