編輯:關於 Android 開發
元件寫完,接著看怎麼用。這一階段只需一個極簡的 Activity 與一份三頁的 XML 版面。
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 的價值:把複雜度關進元件裡。
<?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 也與之整合良好。除非有特殊的版面需求,否則請把力氣花在產品本身,而不是重造一個功能較少的輪子。
安卓第五天筆記:對話框(AlertDialog / 日期 / 進度)
安卓第五天筆記 —— 對話框 1. 常見的對話框家族 AlertDialog:功能最豐富,可承載文字、列表、自訂版面。ProgressDialog:進度對話框。DateP
深入理解 RCU 實現機制:以 2.6.21 核心為例的原始碼剖析
深入理解 RCU 實現 ——基於 2.6.21 核心的 RCU 實現(lvyilong316) RCU(Read-Copy Update),顧名思義就是讀—拷貝—修改,名
React Native 組件生命週期解析:從初始化、更新到卸載
React Native 組件生命週期解析:從初始化、更新到卸載 React Native 的組件也有生命週期。理解這些回調函式,有助於掌握初始化、資料更新與資源清理的時
行動應用支付系列(一):手把手集成支付寶支付
行動應用支付系列(一):手把手集成支付寶支付 在行動應用百花齊放的時代,隨著行動支付的盛行,越來越多的 App 都集成了支付功能。以往支付從不是我負責的模塊,近期專案由我