สร้างรายการใน Wear OS
จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ลองใช้วิธีเขียน
Jetpack Compose ใน Wear OS เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Wear OS
รายการช่วยให้ผู้ใช้เลือกรายการจากชุดตัวเลือกต่างๆ ได้อย่างง่ายดายในอุปกรณ์ Wear OS
ไลบรารี UI ของอุปกรณ์ที่สวมใส่ได้ประกอบด้วยคลาส
WearableRecyclerView
ซึ่งเป็นการใช้งานRecyclerView
เพื่อสร้างรายการที่เพิ่มประสิทธิภาพสำหรับอุปกรณ์ที่สวมใส่ได้ คุณใช้อินเทอร์เฟซนี้ในแอปที่สวมใส่ได้โดยใช้การสร้างคอนเทนเนอร์ WearableRecyclerView
ใหม่
ใช้ WearableRecyclerView
สำหรับรายการแบบยาวที่มีรายการง่ายๆ เช่น ตัวเปิดแอปพลิเคชันหรือรายชื่อติดต่อ แต่ละรายการอาจมีสตริงสั้นๆ และไอคอนที่เชื่อมโยง หรือแต่ละรายการอาจมีเพียงสตริงหรือไอคอน
หมายเหตุ: หลีกเลี่ยงเลย์เอาต์ที่ซับซ้อน ผู้ใช้ควรเห็นไอเทมเพียงแวบเดียวก็เข้าใจได้ว่าเป็นไอเทมอะไร โดยเฉพาะเมื่อใช้กับอุปกรณ์ที่สวมใส่ได้ซึ่งมีหน้าจอขนาดเล็ก
การขยายคลาส RecyclerView
ที่มีอยู่ทำให้ WearableRecyclerView
API แสดงรายการรายการที่เลื่อนในแนวตั้งในรายการแนวตรงโดยค่าเริ่มต้น นอกจากนี้ คุณยังใช้ WearableRecyclerView
API เพื่อเลือกใช้เลย์เอาต์แบบโค้งและท่าทางสัมผัสแบบวงกลมในแอปอุปกรณ์ที่สวมใส่ได้ด้วย
รูปที่ 1
มุมมองรายการเริ่มต้นใน Wear OS
คู่มือนี้จะแสดงวิธีใช้คลาส WearableRecyclerView
เพื่อสร้างรายการในแอป Wear OS, วิธีเลือกใช้เลย์เอาต์โค้งสำหรับรายการที่เลื่อนได้ และวิธีปรับแต่งลักษณะที่ปรากฏของรายการย่อยขณะเลื่อน
เพิ่ม WearableRecyclerView ลงในกิจกรรมโดยใช้ XML
เลย์เอาต์ต่อไปนี้จะเพิ่ม 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);
}
...
}
สร้างเลย์เอาต์แบบโค้ง
หากต้องการสร้างเลย์เอาต์โค้งสำหรับรายการที่เลื่อนได้ในแอปสำหรับอุปกรณ์สวมใส่ ให้ทำดังนี้
-
ใช้
WearableRecyclerView
เป็นคอนเทนเนอร์หลักในเลย์เอาต์ XML ที่เกี่ยวข้อง
-
ตั้งค่าวิธีการ
setEdgeItemsCenteringEnabled(boolean)
เป็น true
ซึ่งจะจัดรายการแรกและรายการสุดท้ายในรายการให้อยู่กึ่งกลางในแนวตั้งบนหน้าจอ
-
ใช้เมธอด
WearableRecyclerView.setLayoutManager()
เพื่อกำหนดเลย์เอาต์ของรายการบนหน้าจอ
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 UTC
[[["เข้าใจง่าย","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 UTC"],[],[],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```"]]