Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
您现在的位置: Android教程網 >> Android系統教程 >  >> Android 技術 >> Android 開發實例

Android ScrollView 探究:可捲動頁面的實作與限制

編輯:Android 開發實例

當畫面上的內容多到一頁放不下時,就需要 ScrollView。它的運作方式是把子元件包起來,內容超出螢幕時就能透過滑動捲動來瀏覽其餘部分。

一個重要限制

ScrollView 只支援垂直捲動。若需要水平滾動,要改用 HorizontalScrollView;若兩個方向都要,就得自己實作或採用其他方案。

實作範例

以下版面配置放入九張圖片,垂直堆疊後必然超出螢幕高度,正好用來展示捲動效果。

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="https://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:scrollbars="vertical">
    <LinearLayout android:orientation="vertical"
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
        <ImageView android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/p1"
            android:layout_gravity="center_horizontal"/>
    </LinearLayout>
</ScrollView>

android:scrollbars="vertical" 明確指定只顯示垂直捲軸,避免視覺上多出一條用不到的線。

兩個容易踩到的地雷

一、ScrollView 只能有一個直接子元件

這是最常見的錯誤。ScrollView 繼承自 FrameLayout,一次只能容納一個子 View。因此範例中必須先包一層 LinearLayout,把所有內容收在裡面。若直接放入多個元件,編譯階段就會報錯。

二、子元件的 layout_height 不能寫 fill_parent

仔細看範例,最外層 ScrollView 的高度是 fill_parent,但內層 LinearLayout 的高度卻也是 fill_parent,這在嚴格情況下會讓內容被壓縮而無法捲動。正確做法有兩種:

  • 把內層的高度改為 wrap_content,讓它依內容自然撐開。
  • 若需要「內容不足時也要填滿螢幕」的效果,則設為 fill_parent 並搭配此用法,讓 ScrollView 依內容與螢幕中較大者決定。

實務上遇到「ScrollView 不能捲」的問題,九成是這個屬性設錯。

什麼時候該用 ListView 而非 ScrollView

若內容是大量結構相同的項目(例如幾百筆資料列表),請用 ListView 或 RecyclerView,不要用 ScrollView 塞 LinearLayout。原因在於 ScrollView 會一次建立全部子 View 並全部配置記憶體,項目一多就會 OOM 或嚴重卡頓;而 ListView / RecyclerView 有 view 回收機制,只保留螢幕上可見的項目。ScrollView 適合的是「數量固定且不多」的內容,例如設定頁、註冊表單。

另外,ScrollView 內部不宜再放 ListView 或其他可捲動元件,兩者的觸碰事件會互相搶奪導致捲動失效。若確實有此需求,需自行處理事件攔截(onInterceptTouchEvent)。

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