จัดทุกอย่างให้เป็นระเบียบอยู่เสมอด้วยคอลเล็กชัน
บันทึกและจัดหมวดหมู่เนื้อหาตามค่ากำหนดของคุณ
ปรับแต่งรายการแบบไดนามิก
ส่วนหนึ่งของ Android Jetpack
ลองใช้ Compose
Jetpack Compose เป็นชุดเครื่องมือ UI ที่แนะนำสำหรับ Android ดูวิธีทำงานกับเลย์เอาต์ใน Compose
คุณปรับแต่ง
RecyclerView
ออบเจ็กต์ให้ตรงกับความต้องการเฉพาะได้ คลาสมาตรฐานที่อธิบายไว้ในสร้างรายการแบบไดนามิกด้วย
RecyclerView มีฟังก์ชันทั้งหมดที่นักพัฒนาแอปส่วนใหญ่ต้องการ ใน
หลายกรณี คุณเพียงแค่ต้องออกแบบมุมมองสำหรับผู้ถือมุมมองแต่ละรายและเขียน
โค้ดเพื่ออัปเดตมุมมองเหล่านั้นด้วยข้อมูลที่เหมาะสม อย่างไรก็ตาม หากแอปมี
ข้อกำหนดเฉพาะ คุณสามารถแก้ไขลักษณะการทำงานมาตรฐานได้หลายวิธี
เอกสารนี้จะอธิบายการปรับแต่งบางอย่างที่อาจทำได้
แก้ไขเลย์เอาต์
RecyclerView
ใช้เครื่องมือจัดการเลย์เอาต์เพื่อจัดตำแหน่งแต่ละรายการบนหน้าจอ และเพื่อกำหนดเวลาที่จะนำมุมมองรายการที่ผู้ใช้มองไม่เห็นอีกต่อไปมาใช้ซ้ำ หากต้องการนำมุมมองกลับมาใช้ซ้ำหรือรีไซเคิล มุมมองนั้น ตัวจัดการเลย์เอาต์
อาจขอให้อะแดปเตอร์แทนที่เนื้อหาของมุมมองด้วย
องค์ประกอบอื่นจากชุดข้อมูล การรีไซเคิลการดูด้วยวิธีนี้จะช่วยปรับปรุง
ประสิทธิภาพโดยหลีกเลี่ยงการสร้างการดูที่ไม่จำเป็นหรือการดำเนินการ
ที่มีค่าใช้จ่ายสูง
findViewById()
ไลบรารีการสนับสนุนของ Android มีเครื่องมือจัดการเลย์เอาต์มาตรฐาน 3 รายการ
ซึ่งแต่ละรายการมีตัวเลือกการปรับแต่งมากมาย
LinearLayoutManager
จัดเรียงรายการในลิสต์แบบ 1 มิติ การใช้
RecyclerView
กับ LinearLayoutManager
จะมีฟังก์ชันการทำงาน
เหมือนกับเลย์เอาต์ ListView
GridLayoutManager
จัดเรียงรายการในตาราง 2 มิติ เช่น สี่เหลี่ยมจัตุรัสบน
กระดานหมากรุก การใช้ RecyclerView
กับ
GridLayoutManager
จะมีฟังก์ชันการทำงานเหมือนกับเลย์เอาต์ GridView
StaggeredGridLayoutManager
:
จัดเรียงรายการในตาราง 2 มิติ โดยแต่ละคอลัมน์จะออฟเซ็ตเล็กน้อย
จากคอลัมน์ก่อนหน้า เช่นเดียวกับดาวบนธงชาติอเมริกา
หากตัวจัดการเลย์เอาต์เหล่านี้ไม่เหมาะกับความต้องการ คุณก็สร้างตัวจัดการเลย์เอาต์ของคุณเองได้โดย
ขยาย
RecyclerView.LayoutManager
คลาสแบบนามธรรม
เพิ่มภาพเคลื่อนไหวของรายการ
เมื่อใดก็ตามที่ไอเทมมีการเปลี่ยนแปลง RecyclerView
จะใช้แอนิเมเตอร์
เพื่อเปลี่ยนลักษณะที่ปรากฏ Animator นี้เป็นออบเจ็กต์ที่ขยายคลาส
RecyclerView.ItemAnimator
นามธรรม โดยค่าเริ่มต้น RecyclerView
จะใช้
DefaultItemAnimator
เพื่อแสดงภาพเคลื่อนไหว หากต้องการระบุภาพเคลื่อนไหวที่กำหนดเอง คุณสามารถ
กำหนดออบเจ็กต์ Animator ของคุณเองได้โดยการขยาย
RecyclerView.ItemAnimator
เปิดใช้การเลือกรายการ
ไลบรารี
recyclerview-selection
ช่วยให้ผู้ใช้เลือกรายการในลิสต์ RecyclerView
โดยใช้การป้อนข้อมูลด้วยการสัมผัส
หรือเมาส์ ซึ่งจะช่วยให้คุณควบคุมการนำเสนอภาพของ
รายการที่เลือกได้ นอกจากนี้ คุณยังคงควบคุมนโยบายที่ควบคุมลักษณะการทำงานของการเลือกได้ เช่น รายการใดมีสิทธิ์สำหรับการเลือกและเลือกได้กี่รายการ
หากต้องการเพิ่มการรองรับการเลือกไปยังอินสแตนซ์ RecyclerView
ให้ทำตามขั้นตอนต่อไปนี้
- กำหนดประเภทคีย์การเลือกที่จะใช้ แล้วสร้าง
ItemKeyProvider
คุณใช้ประเภทคีย์หลัก 3 ประเภทเพื่อระบุรายการที่เลือกได้ ดังนี้
ดูข้อมูลโดยละเอียดเกี่ยวกับประเภทคีย์การเลือกได้ที่
SelectionTracker.Builder
- ใช้งาน
ItemDetailsLookup
ItemDetailsLookup
อนุญาตให้คลังการเลือกเข้าถึง
ข้อมูลเกี่ยวกับรายการ RecyclerView
โดยใช้ MotionEvent
ซึ่งเป็นเหมือนโรงงานสำหรับอินสแตนซ์ ItemDetails
ที่ได้รับการสำรองข้อมูลหรือดึงข้อมูลจากอินสแตนซ์ RecyclerView.ViewHolder
- อัปเดตออบเจ็กต์
View
ใน RecyclerView
เพื่อแสดงว่าผู้ใช้เลือกหรือยกเลิกการเลือกออบเจ็กต์
ไลบรารีการเลือกไม่ได้ตกแต่งภาพเริ่มต้นสำหรับ
รายการที่เลือก ระบุข้อมูลนี้เมื่อคุณติดตั้งใช้งาน
onBindViewHolder()
เราขอแนะนำให้ใช้วิธีต่อไปนี้
- ใน
onBindViewHolder()
ให้เรียก
setActivated()
—ไม่ใช่
setSelected()
—ใน
ออบเจ็กต์ View
ด้วย
true
หรือ false
ขึ้นอยู่กับว่ามีการเลือกรายการ
หรือไม่
- อัปเดตสไตล์ของมุมมองเพื่อแสดงสถานะที่เปิดใช้งาน เรา
ขอแนะนำให้ใช้ทรัพยากรรายการสถานะสีเพื่อกำหนดค่าการจัดรูปแบบ
- ใช้
ActionMode
เพื่อมอบเครื่องมือให้ผู้ใช้ดำเนินการกับการเลือก
ลงทะเบียน
SelectionTracker.SelectionObserver
เพื่อรับการแจ้งเตือนเมื่อมีการเปลี่ยนแปลงการเลือก เมื่อสร้างการเลือกเป็นครั้งแรก ให้เริ่ม ActionMode
เพื่อแสดงการเลือกนี้ต่อผู้ใช้และระบุ
การดำเนินการเฉพาะการเลือก เช่น คุณสามารถเพิ่มปุ่มลบลงใน
ActionMode
แถบและเชื่อมต่อลูกศรย้อนกลับในแถบเพื่อล้าง
การเลือก เมื่อไม่มีการเลือก (หากผู้ใช้ล้างการเลือกครั้งล่าสุด) ให้สิ้นสุดโหมดการดำเนินการ
- ดำเนินการรองที่ตีความแล้ว
ที่ส่วนท้ายของไปป์ไลน์การประมวลผลเหตุการณ์ ไลบรารีอาจพิจารณา
ว่าผู้ใช้พยายามเปิดใช้งานรายการโดยการแตะ หรือพยายาม
ลากรายการหรือชุดรายการที่เลือก ตอบสนองต่อการตีความเหล่านี้
โดยลงทะเบียน Listener ที่เหมาะสม ดูข้อมูลเพิ่มเติมได้ที่
SelectionTracker.Builder
- ประกอบทุกอย่างด้วย
SelectionTracker.Builder
ตัวอย่างต่อไปนี้แสดงวิธีรวมชิ้นส่วนเหล่านี้เข้าด้วยกัน
Kotlin
var tracker = SelectionTracker.Builder(
"my-selection-id",
recyclerView,
StableIdKeyProvider(recyclerView),
MyDetailsLookup(recyclerView),
StorageStrategy.createLongStorage())
.withOnItemActivatedListener(myItemActivatedListener)
.build()
Java
SelectionTracker tracker = new SelectionTracker.Builder<>(
"my-selection-id",
recyclerView,
new StableIdKeyProvider(recyclerView),
new MyDetailsLookup(recyclerView),
StorageStrategy.createLongStorage())
.withOnItemActivatedListener(myItemActivatedListener)
.build();
หากต้องการสร้างอินสแตนซ์
SelectionTracker
แอปของคุณต้องระบุ
RecyclerView.Adapter
เดียวกันกับที่ใช้เริ่มต้น RecyclerView
เป็น
SelectionTracker.Builder
ด้วยเหตุนี้ หลังจากสร้างอินสแตนซ์
SelectionTracker
แล้ว ให้แทรกลงใน
RecyclerView.Adapter
มิฉะนั้น คุณจะตรวจสอบสถานะที่เลือกของสินค้าจากonBindViewHolder()
ไม่ได้
- รวมการเลือกในเหตุการณ์วงจร
กิจกรรม
หากต้องการรักษาสถานะการเลือกในเหตุการณ์วงจรของกิจกรรม แอปของคุณ
ต้องเรียกใช้
onSaveInstanceState()
และ
onRestoreInstanceState()
ของเครื่องมือติดตามการเลือกจากเมธอด
onSaveInstanceState()
และ
onRestoreInstanceState()
ของกิจกรรมตามลำดับ แอปของคุณต้องระบุรหัสการเลือกที่ไม่ซ้ำกันให้กับ
SelectionTracker.Builder
ตัวสร้างด้วย ต้องใช้รหัสนี้เนื่องจาก
กิจกรรมหรือ Fragment อาจมีรายการที่เลือกได้และแตกต่างกันมากกว่า 1 รายการ
ซึ่งทั้งหมดต้องคงอยู่ในสถานะที่บันทึกไว้
แหล่งข้อมูลเพิ่มเติม
ดูข้อมูลเพิ่มเติมได้ที่แหล่งข้อมูลต่อไปนี้
ตัวอย่างเนื้อหาและโค้ดในหน้าเว็บนี้ขึ้นอยู่กับใบอนุญาตที่อธิบายไว้ในใบอนุญาตการใช้เนื้อหา Java และ OpenJDK เป็นเครื่องหมายการค้าหรือเครื่องหมายการค้าจดทะเบียนของ Oracle และ/หรือบริษัทในเครือ
อัปเดตล่าสุด 2025-08-27 UTC
[null,null,["อัปเดตล่าสุด 2025-08-27 UTC"],[],[],null,["# Customize a dynamic list\nPart of [Android Jetpack](/jetpack).\n=============================================================\n\nTry the Compose way \nJetpack Compose is the recommended UI toolkit for Android. Learn how to work with layouts in Compose. \n[Lazy Lists and Grids →](/jetpack/compose/lists#lazy) \n\nYou can customize\n[RecyclerView](/reference/androidx/recyclerview/widget/RecyclerView)\nobjects to meet your specific needs. The standard classes described in\n[Create dynamic lists with\nRecyclerView](/guide/topics/ui/layout/recyclerview) provide all the functionality that most developers need. In\nmany cases, you only need to design the view for each view holder and write the\ncode to update those views with the appropriate data. However, if your app has\nspecific requirements, you can modify the standard behavior in a number of ways.\nThis document describes some of the possible customizations.\n\nModify the layout\n-----------------\n\n`RecyclerView` uses a layout manager to position the individual\nitems on the screen and to determine when to reuse item views that are no longer\nvisible to the user. To reuse---or *recycle* ---a view, a layout\nmanager might ask the adapter to replace the contents of the view with a\ndifferent element from the dataset. Recycling views this way improves\nperformance by avoiding the creation of unnecessary views or performing\nexpensive\n[findViewById()](/reference/android/app/Activity#findViewById(int))\nlookups. The Android Support Library includes three standard layout managers,\nach of which offers many customization options:\n\n- [LinearLayoutManager](/reference/androidx/recyclerview/widget/LinearLayoutManager): arranges the items in a one-dimensional list. Using a `RecyclerView` with `LinearLayoutManager` provides functionality like a [ListView](/reference/android/widget/ListView) layout.\n- [GridLayoutManager](/reference/androidx/recyclerview/widget/GridLayoutManager): arranges the items in a two-dimensional grid, like the squares on a checkerboard. Using a `RecyclerView` with `GridLayoutManager` provides functionality like a [GridView](/reference/android/widget/GridView) layout.\n- [StaggeredGridLayoutManager](/reference/androidx/recyclerview/widget/StaggeredGridLayoutManager): arranges the items in a two-dimensional grid, with each column slightly offset from the one before, like the stars on an American flag.\n\nIf these layout managers don't suit your needs, you can create your own by\nextending the\n[RecyclerView.LayoutManager](/reference/androidx/recyclerview/widget/RecyclerView.LayoutManager)\nabstract class.\n\nAdd item animations\n-------------------\n\nWhenever an item changes, `RecyclerView` uses an *animator*\nto change its appearance. This animator is an object that extends the abstract\n[RecyclerView.ItemAnimator](/reference/androidx/recyclerview/widget/RecyclerView.ItemAnimator)\nclass. By default, the `RecyclerView` uses\n[DefaultItemAnimator](/reference/androidx/recyclerview/widget/DefaultItemAnimator)\nto provide the animation. If you want to provide custom animations, you can\ndefine your own animator object by extending\n`RecyclerView.ItemAnimator`.\n\nEnable list-item selection\n--------------------------\n\nThe\n[`recyclerview-selection`](/reference/androidx/recyclerview/selection/package-summary)\nlibrary lets users select items in a `RecyclerView` list using touch\nor mouse input. This lets you retain control over the visual presentation of a\nselected item. You can also retain control over policies controlling selection\nbehavior, such as which items are eligible for selection and how many items can\nbe selected.\n\nTo add selection support to a `RecyclerView` instance, follow\nthese steps:\n\n1. Determine which selection key type to use, then build an [`ItemKeyProvider`](/reference/androidx/recyclerview/selection/ItemKeyProvider).\n\n There are three key types you can use to identify selected items:\n - [Parcelable](/reference/android/os/Parcelable) and its subclasses, like [Uri](/reference/android/net/Uri)\n - [String](/reference/java/lang/String)\n - [Long](/reference/java/lang/Long)\n\n For detailed information about selection-key types, see\n [SelectionTracker.Builder](/reference/androidx/recyclerview/selection/SelectionTracker.Builder).\n2. Implement [ItemDetailsLookup](/reference/androidx/recyclerview/selection/ItemDetailsLookup).\n3. `ItemDetailsLookup` lets the selection library access information about `RecyclerView` items given a [MotionEvent](/reference/android/view/MotionEvent). It is effectively a factory for [`ItemDetails`](/reference/androidx/recyclerview/selection/ItemDetailsLookup.ItemDetails) instances that are backed up by, or extracted from, a [RecyclerView.ViewHolder](/reference/androidx/recyclerview/widget/RecyclerView.ViewHolder) instance.\n4. Update item [View](/reference/android/view/View) objects in the `RecyclerView` to reflect whether the user selects or unselects them.\n\n The selection library doesn't provide a default visual decoration for the\n selected items. Provide this when you implement\n [onBindViewHolder()](/reference/androidx/recyclerview/widget/RecyclerView.Adapter#onBindViewHolder(VH, int)).\n We recommend the following approach:\n - In `onBindViewHolder()`, call [setActivated()](/reference/android/view/View#setActivated(boolean))---**not** [setSelected()](/reference/android/view/View#setSelected(boolean))---on the `View` object with `true` or `false`, depending on whether the item is selected.\n - Update the styling of the view to represent the activated status. We recommend using a [color state\n list resource](/guide/topics/resources/color-list-resource) to configure the styling.\n5. Use [ActionMode](/reference/androidx/appcompat/view/ActionMode) to provide the user with tools to perform an action on the selection.\n6. Register a [SelectionTracker.SelectionObserver](/reference/androidx/recyclerview/selection/SelectionTracker.SelectionObserver) to be notified when a selection changes. When a selection is first created, start `ActionMode` to present this to the user and provide selection-specific actions. For example, you can add a delete button to the `ActionMode` bar and connect the back arrow on the bar to clear the selection. When the selection becomes empty---if the user clears the selection the last time---terminate action mode.\n7. Perform any interpreted secondary actions.\n8. At the end of the event processing pipeline, the library might determine that the user is attempting to activate an item, by tapping it, or is attempting to drag an item or set of selected items. React to these interpretations by registering the appropriate listener. For more information, see [SelectionTracker.Builder](/reference/androidx/recyclerview/selection/SelectionTracker.Builder).\n9. Assemble everything with `SelectionTracker.Builder`.\n10. The following example shows how to put these pieces together: \n\n### Kotlin\n\n```kotlin\n var tracker = SelectionTracker.Builder(\n \"my-selection-id\",\n recyclerView,\n StableIdKeyProvider(recyclerView),\n MyDetailsLookup(recyclerView),\n StorageStrategy.createLongStorage())\n .withOnItemActivatedListener(myItemActivatedListener)\n .build()\n \n```\n\n### Java\n\n```java\n SelectionTracker tracker = new SelectionTracker.Builder\u003c\u003e(\n \"my-selection-id\",\n recyclerView,\n new StableIdKeyProvider(recyclerView),\n new MyDetailsLookup(recyclerView),\n StorageStrategy.createLongStorage())\n .withOnItemActivatedListener(myItemActivatedListener)\n .build();\n \n```\n11. To build a [SelectionTracker](/reference/androidx/recyclerview/selection/SelectionTracker) instance, your app must supply the same [RecyclerView.Adapter](/reference/androidx/recyclerview/widget/RecyclerView.Adapter) that you use to initialize `RecyclerView` to `SelectionTracker.Builder`. For this reason, after you create the `SelectionTracker` instance, inject it into your `RecyclerView.Adapter`. Otherwise, you can't check an item's selected status from the `onBindViewHolder()` method.\n12. Include selection in the [activity\n lifecycle](/guide/components/activities/activity-lifecycle) events.\n13. To preserve selection state across the activity lifecycle events, your app must call the selection tracker's [onSaveInstanceState()](/reference/androidx/recyclerview/selection/SelectionTracker#onSaveInstanceState(android.os.Bundle)) and [onRestoreInstanceState()](/reference/androidx/recyclerview/selection/SelectionTracker#onRestoreInstanceState(android.os.Bundle)) methods from the activity's [onSaveInstanceState()](/reference/android/app/Activity#onSaveInstanceState(android.os.Bundle)) and [onRestoreInstanceState()](/reference/android/app/Activity#onRestoreInstanceState(android.os.Bundle)) methods, respectively. Your app must also supply a unique selection ID to the `SelectionTracker.Builder` constructor. This ID is required because an activity or a fragment might have more than one distinct, selectable list, all of which need to be persisted in their saved state.\n\nAdditional resources\n--------------------\n\nSee the following references for additional information.\n\n- [Sunflower\n demo app](https://github.com/googlesamples/android-sunflower), which uses `RecyclerView`.\n- [Use\n RecyclerView to display a scrollable list](/codelabs/basic-android-kotlin-training-recyclerview-scrollable-list#0) codelab.\n- [Android\n Kotlin Fundamentals: RecyclerView fundamentals](/codelabs/kotlin-android-training-recyclerview-fundamentals) codelab."]]