在 Wear OS 上建立清單
透過集合功能整理內容
你可以依據偏好儲存及分類內容。
試試 Compose 的方式
建議您使用 Wear OS 版 Jetpack Compose,這是 Wear OS 的推薦 UI 工具包。
使用者可透過清單功能,輕鬆在 Wear OS 裝置上從一組選項中選取所需項目。
Wearable UI 程式庫內含 WearableRecyclerView
類別,此為 RecyclerView
的實作成果,用來建立為穿戴式裝置進行最佳化調整的清單。只要建立新的 WearableRecyclerView
容器,即可在穿戴式應用程式中使用此介面。
如果是較長的簡單項目清單,例如應用程式啟動器或聯絡人清單,請使用 WearableRecyclerView
。每個項目都可能有一個短字串和一個相關聯的圖示。此外,每個項目也可能只有一個字串或圖示。
注意:請避免使用複雜的版面配置,讓使用者能立即瞭解項目的用途,特別是螢幕尺寸有限的穿戴式裝置更應如此。
藉由擴充現有的 RecyclerView
類別,在預設狀態下,WearableRecyclerView
API 會在直向清單中垂直顯示一系列可捲動的項目。此外,您也可以使用 WearableRecyclerView
API,在穿戴式應用程式中導入曲線版面配置及圓形捲動手勢。
圖 1:Wear OS 的預設清單檢視畫面。
本指南將說明如何使用 WearableRecyclerView
類別在 Wear OS 應用程式中建立清單、如何為可捲動的項目選擇採用弧形版面配置,以及如何自訂捲動畫面時的子項外觀。
使用 XML 將 WearableRecyclerView 新增至活動
下列版面配置會將 WearableRecyclerView
新增至活動。
<androidx.wear.widget.WearableRecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/recycler_launcher_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
以下範例為套用至活動的 WearableRecyclerView
:
Kotlin
class MainActivity : Activity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
}
...
}
Java
public class MainActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
...
}
建立弧形版面配置
如要為穿戴式應用程式中的可捲動項目建立弧形版面配置,請按照下列步驟操作:
Kotlin
wearableRecyclerView.apply {
// To align the edge children (first and last) with the center of the screen.
isEdgeItemsCenteringEnabled = true
...
layoutManager = WearableLinearLayoutManager(this@MainActivity)
}
Java
// To align the edge children (first and last) with the center of the screen.
wearableRecyclerView.setEdgeItemsCenteringEnabled(true);
...
wearableRecyclerView.setLayoutManager(
new WearableLinearLayoutManager(this));
如果應用程式有自訂捲動時顯示的子項外觀的特定需求 (例如當項目因畫面捲動而離開畫面中心時,讓圖示和文字隨著畫面捲動縮放),請擴充
WearableLinearLayoutManager.LayoutCallback
類別並覆寫
onLayoutFinished
方法。
以下程式碼片段範例顯示如何擴充 WearableLinearLayoutManager.LayoutCallback
類別來自訂項目捲動方式,縮放遠離中心的項目:
Kotlin
/** How much icons should scale, at most. */
private const val MAX_ICON_PROGRESS = 0.65f
class CustomScrollingLayoutCallback : WearableLinearLayoutManager.LayoutCallback() {
private var progressToCenter: Float = 0f
override fun onLayoutFinished(child: View, parent: RecyclerView) {
child.apply {
// Figure out % progress from top to bottom.
val centerOffset = height.toFloat() / 2.0f / parent.height.toFloat()
val yRelativeToCenterOffset = y / parent.height + centerOffset
// Normalize for center.
progressToCenter = Math.abs(0.5f - yRelativeToCenterOffset)
// Adjust to the maximum scale.
progressToCenter = Math.min(progressToCenter, MAX_ICON_PROGRESS)
scaleX = 1 - progressToCenter
scaleY = 1 - progressToCenter
}
}
}
Java
public class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {
/** How much icons should scale, at most. */
private static final float MAX_ICON_PROGRESS = 0.65f;
private float progressToCenter;
@Override
public void onLayoutFinished(View child, RecyclerView parent) {
// Figure out % progress from top to bottom.
float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();
float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;
// Normalize for center.
progressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);
// Adjust to the maximum scale.
progressToCenter = Math.min(progressToCenter, MAX_ICON_PROGRESS);
child.setScaleX(1 - progressToCenter);
child.setScaleY(1 - progressToCenter);
}
}
Kotlin
wearableRecyclerView.layoutManager =
WearableLinearLayoutManager(this, CustomScrollingLayoutCallback())
Java
CustomScrollingLayoutCallback customScrollingLayoutCallback =
new CustomScrollingLayoutCallback();
wearableRecyclerView.setLayoutManager(
new WearableLinearLayoutManager(this, customScrollingLayoutCallback));
這個頁面中的內容和程式碼範例均受《內容授權》中的授權所規範。Java 與 OpenJDK 是 Oracle 和/或其關係企業的商標或註冊商標。
上次更新時間:2025-07-26 (世界標準時間)。
[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["缺少我需要的資訊","missingTheInformationINeed","thumb-down"],["過於複雜/步驟過多","tooComplicatedTooManySteps","thumb-down"],["過時","outOfDate","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["示例/程式碼問題","samplesCodeIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-07-26 (世界標準時間)。"],[],[],null,["# Create lists on Wear OS\n\nTry the Compose way \nJetpack Compose on Wear OS is the recommended UI toolkit for Wear OS. \n[Create lists using Compose on Wear OS →](/training/wearables/compose/lists) \n\n\nLists let users select an item from a set of choices easily on Wear OS devices.\n\n\nThe Wearable UI Library includes the\n[`WearableRecyclerView`](/reference/androidx/wear/widget/WearableRecyclerView) class, which is a\n[`RecyclerView`](/reference/androidx/recyclerview/widget/RecyclerView)\nimplementation for creating lists optimized for wearable devices. You can use this\ninterface in your wearable app by creating a new `WearableRecyclerView` container.\n\n\nUse a `WearableRecyclerView` for a\nlong list of simple items, such as an application launcher or a list of contacts. Each item might\nhave a short string and an associated icon. Alternatively, each item might have only a string\nor an icon.\n\n\n**Note:** Avoid complex layouts. Users should only need to glance at an item to\nunderstand what it is, especially with wearables' limited screen size.\n\n\nBy extending the existing `RecyclerView` class, `WearableRecyclerView`\nAPIs display a vertically scrollable list of items in a straight list by default. You can also use\nthe `WearableRecyclerView` APIs to opt-in for a curved layout and\na [circular scrolling gesture](/reference/androidx/wear/widget/WearableRecyclerView#setCircularScrollingGestureEnabled(boolean))\nin your wearable apps.\n\n**Figure 1.**\nDefault list view on Wear OS.\n\n\nThis guide shows you how to use the `WearableRecyclerView` class to create\nlists in your Wear OS apps, how to opt-in for a curved layout\nfor your scrollable items, and how to customize the appearance of\nthe children while scrolling.\n\nAdd WearableRecyclerView to an activity using XML\n-------------------------------------------------\n\n\nThe following layout adds a `WearableRecyclerView` to an activity: \n\n```xml\n\u003candroidx.wear.widget.WearableRecyclerView\n xmlns:android=\"http://schemas.android.com/apk/res/android\"\n xmlns:tools=\"http://schemas.android.com/tools\"\n android:id=\"@+id/recycler_launcher_view\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\n android:scrollbars=\"vertical\" /\u003e\n```\n\n\nThe following example shows the `WearableRecyclerView`\napplied to an activity: \n\n### Kotlin\n\n```kotlin\nclass MainActivity : Activity() {\n\n override fun onCreate(savedInstanceState: Bundle?) {\n super.onCreate(savedInstanceState)\n setContentView(R.layout.activity_main)\n }\n ...\n}\n```\n\n### Java\n\n```java\npublic class MainActivity extends Activity {\n @Override\n public void onCreate(Bundle savedInstanceState) {\n super.onCreate(savedInstanceState);\n setContentView(R.layout.activity_main);\n }\n ...\n}\n```\n\nCreate a curved layout\n----------------------\n\n\nTo create a curved layout for scrollable items in your wearable app, do the following:\n\n- Use [`WearableRecyclerView`](/reference/androidx/wear/widget/WearableRecyclerView) as your main container in the relevant XML layout.\n- Set the [`setEdgeItemsCenteringEnabled(boolean)`](/reference/androidx/wear/widget/WearableRecyclerView#setEdgeItemsCenteringEnabled(boolean)) method to `true`. This vertically centers the first and last items on the list on the screen.\n- Use the `WearableRecyclerView.setLayoutManager()` method to set the layout of the items on the screen.\n\n### Kotlin\n\n```kotlin\nwearableRecyclerView.apply {\n // To align the edge children (first and last) with the center of the screen.\n isEdgeItemsCenteringEnabled = true\n ...\n layoutManager = WearableLinearLayoutManager(this@MainActivity)\n}\n```\n\n### Java\n\n```java\n// To align the edge children (first and last) with the center of the screen.\nwearableRecyclerView.setEdgeItemsCenteringEnabled(true);\n...\nwearableRecyclerView.setLayoutManager(\n new WearableLinearLayoutManager(this));\n```\n\n\nIf your app has specific requirements to customize the appearance of the children while scrolling---for example,\nscaling the icons and text while the items scroll away from the center---extend\nthe [`WearableLinearLayoutManager.LayoutCallback`](/reference/androidx/wear/widget/WearableLinearLayoutManager.LayoutCallback) class and override the\n[`onLayoutFinished`](/reference/androidx/wear/widget/WearableLinearLayoutManager.LayoutCallback#onLayoutFinished(android.view.View, androidx.recyclerview.widget.RecyclerView)) method.\n\n\nThe following code snippets show an example of customizing the scrolling of items to scale\nfarther away from the center by extending the\n`WearableLinearLayoutManager.LayoutCallback` class: \n\n### Kotlin\n\n```kotlin\n/** How much icons should scale, at most. */\nprivate const val MAX_ICON_PROGRESS = 0.65f\n\nclass CustomScrollingLayoutCallback : WearableLinearLayoutManager.LayoutCallback() {\n\n private var progressToCenter: Float = 0f\n\n override fun onLayoutFinished(child: View, parent: RecyclerView) {\n child.apply {\n // Figure out % progress from top to bottom.\n val centerOffset = height.toFloat() / 2.0f / parent.height.toFloat()\n val yRelativeToCenterOffset = y / parent.height + centerOffset\n\n // Normalize for center.\n progressToCenter = Math.abs(0.5f - yRelativeToCenterOffset)\n // Adjust to the maximum scale.\n progressToCenter = Math.min(progressToCenter, MAX_ICON_PROGRESS)\n\n scaleX = 1 - progressToCenter\n scaleY = 1 - progressToCenter\n }\n }\n}\n```\n\n### Java\n\n```java\npublic class CustomScrollingLayoutCallback extends WearableLinearLayoutManager.LayoutCallback {\n /** How much icons should scale, at most. */\n private static final float MAX_ICON_PROGRESS = 0.65f;\n\n private float progressToCenter;\n\n @Override\n public void onLayoutFinished(View child, RecyclerView parent) {\n\n // Figure out % progress from top to bottom.\n float centerOffset = ((float) child.getHeight() / 2.0f) / (float) parent.getHeight();\n float yRelativeToCenterOffset = (child.getY() / parent.getHeight()) + centerOffset;\n\n // Normalize for center.\n progressToCenter = Math.abs(0.5f - yRelativeToCenterOffset);\n // Adjust to the maximum scale.\n progressToCenter = Math.min(progressToCenter, MAX_ICON_PROGRESS);\n\n child.setScaleX(1 - progressToCenter);\n child.setScaleY(1 - progressToCenter);\n }\n}\n``` \n\n### Kotlin\n\n```kotlin\nwearableRecyclerView.layoutManager =\n WearableLinearLayoutManager(this, CustomScrollingLayoutCallback())\n```\n\n### Java\n\n```java\nCustomScrollingLayoutCallback customScrollingLayoutCallback =\n new CustomScrollingLayoutCallback();\nwearableRecyclerView.setLayoutManager(\n new WearableLinearLayoutManager(this, customScrollingLayoutCallback));\n```"]]