با مجموعهها، منظم بمانید
ذخیره و طبقهبندی محتوا براساس اولویتهای شما.
RecyclerView یک کامپوننت View است که نمایش کارآمد مجموعه های بزرگ داده را آسان می کند. RecyclerView به جای ایجاد نماها برای هر مورد در مجموعه داده، عملکرد برنامه شما را با نگه داشتن مجموعه کوچکی از نماها و بازیافت آنها در حین پیمایش در آن موارد، بهبود می بخشد.
در Compose، می توانید از لیست های تنبل برای انجام همان کار استفاده کنید. این صفحه توضیح میدهد که چگونه میتوانید پیادهسازی RecyclerView خود را برای استفاده از لیستهای تنبل در Compose تغییر دهید.
مراحل مهاجرت
برای انتقال پیادهسازی RecyclerView به Compose، این مراحل را دنبال کنید:
RecyclerView نظر دهید یا از سلسله مراتب رابط کاربری خود حذف کنید و اگر هنوز هیچ کدام در سلسله مراتب وجود ندارد، یک ComposeView اضافه کنید تا جایگزین آن شود. این محفظه ای برای لیست تنبل است که اضافه می کنید:
بر اساس مدیر طرحبندی RecyclerView خود، نوع فهرست Lazy قابل تنظیم را تعیین کنید (جدول زیر را ببینید). Composable که انتخاب میکنید، ComposeView ی است که در مرحله قبل اضافه کردهاید.
LayoutManager
ترکیب پذیر
LinearLayoutManager
LazyColumn یا LazyRow
GridLayoutManager
LazyVerticalGrid یا LazyHorizontalGrid
StaggeredGridLayoutManager
LazyVerticalStaggeredGrid یا LazyHorizontalStaggeredGrid
// recyclerView.layoutManager = LinearLayoutManager(context)composeView.setContent{LazyColumn(Modifier.fillMaxSize()){// We use a LazyColumn since the layout manager of the RecyclerView is a vertical LinearLayoutManager}}
در پیاده سازی RecyclerView.Adapter خود، یک composable مربوطه برای هر نوع نمای ایجاد کنید. هر نوع نمای معمولاً به یک زیر کلاس ViewHolder نگاشت می شود، اگرچه ممکن است همیشه اینطور نباشد. این ترکیبپذیرها بهعنوان نمایش رابط کاربری برای انواع مختلف عناصر در فهرست شما استفاده خواهند شد:
@ComposablefunListItem(data:MyData,modifier:Modifier=Modifier){Row(modifier.fillMaxWidth()){Text(text=data.name)// … other composables required for displaying `data`}}
منطق متدهای onCreateViewHolder() و onBindViewHolder()RecyclerView.Adapter شما با این composable ها و حالتی که برای آنها ارائه می کنید جایگزین می شود. در Compose، هیچ جدایی بین ایجاد یک composable برای یک آیتم و اتصال داده به آن وجود ندارد - این مفاهیم با هم ترکیب می شوند.
در شکاف content لیست تنبل (پارامتر لامبدا انتهایی)، از تابع items() (یا یک اضافه بار معادل) برای تکرار در داده های لیست خود استفاده کنید. در itemContent lambda، مورد قابل ترکیب مناسب را برای داده های خود فراخوانی کنید:
RecyclerView مفهوم ItemDecoration را دارد که می توانید از آن برای اضافه کردن یک نقاشی خاص برای آیتم های موجود در لیست استفاده کنید. به عنوان مثال، میتوانید یک ItemDecoration برای اضافه کردن تقسیمکنندهها بین آیتمها اضافه کنید:
Compose مفهومی معادل از تزئینات آیتم ندارد. درعوض، میتوانید هر گونه تزئینات رابط کاربری را در لیست مستقیماً در ترکیب اضافه کنید. به عنوان مثال، برای افزودن تقسیم کننده ها به لیست، می توانید بعد از هر آیتم Divider composable استفاده کنید:
یک ItemAnimator می توان روی RecyclerView تنظیم کرد تا ظاهر آیتم ها را با ایجاد تغییرات در آداپتور متحرک کند. بهطور پیشفرض، RecyclerView از DefaultItemAnimator استفاده میکند که انیمیشنهای اولیه را برای حذف، افزودن و جابجایی رویدادها ارائه میکند.
لیست های تنبل از طریق اصلاح کننده animateItemPlacement مفهوم مشابهی دارند. برای کسب اطلاعات بیشتر به انیمیشن های آیتم ها مراجعه کنید.
منابع اضافی
برای اطلاعات بیشتر در مورد انتقال RecyclerView به Compose، به منابع زیر مراجعه کنید:
فهرستها و شبکهها : مستنداتی برای نحوه پیادهسازی فهرستها و شبکهها در Compose.
محتوا و نمونه کدها در این صفحه مشمول پروانههای توصیفشده در پروانه محتوا هستند. جاوا و OpenJDK علامتهای تجاری یا علامتهای تجاری ثبتشده Oracle و/یا وابستههای آن هستند.
تاریخ آخرین بهروزرسانی 2025-08-22 بهوقت ساعت هماهنگ جهانی.
[null,null,["تاریخ آخرین بهروزرسانی 2025-08-22 بهوقت ساعت هماهنگ جهانی."],[],[],null,["[`RecyclerView`](/develop/ui/views/layout/recyclerview) is a View component that makes it easy to efficiently display\nlarge sets of data. Instead of creating views for each item in the data set,\n`RecyclerView` improves the performance of your app by keeping a small pool of\nviews and recycling through them as you scroll through those items.\n\nIn Compose, you can use [Lazy lists](/develop/ui/compose/lists#lazy) to accomplish the same thing. This page\ndescribes how you can migrate your `RecyclerView` implementation to use Lazy lists\nin Compose.\n\nMigration steps\n\nTo migrate your `RecyclerView` implementation to Compose, follow these steps:\n\n1. Comment out or remove the `RecyclerView` from your UI hierarchy and add a\n `ComposeView` to replace it if none is present in the hierarchy yet. This\n is the container for the Lazy list that you'll add:\n\n \u003cFrameLayout\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\"\u003e\n\n \u003c!-- \u003candroidx.recyclerview.widget.RecyclerView--\u003e\n \u003c!-- android:id=\"@+id/recycler_view\"--\u003e\n \u003c!-- android:layout_width=\"match_parent\"--\u003e\n \u003c!-- android:layout_height=\"match_parent /\u003e\"--\u003e\n\n \u003candroidx.compose.ui.platform.ComposeView\n android:id=\"@+id/compose_view\"\n android:layout_width=\"match_parent\"\n android:layout_height=\"match_parent\" /\u003e\n\n \u003c/FrameLayout\u003e\n\n2. Determine what type of Lazy list composable you need based on your\n `RecyclerView`'s layout manager (see table below). The composable you select\n will be the top-level composable of the `ComposeView` you added in the\n previous step.\n\n | `LayoutManager` | Composable |\n |------------------------------|--------------------------------------------------------------|\n | `LinearLayoutManager` | `LazyColumn` or `LazyRow` |\n | `GridLayoutManager` | `LazyVerticalGrid` or `LazyHorizontalGrid` |\n | `StaggeredGridLayoutManager` | `LazyVerticalStaggeredGrid` or `LazyHorizontalStaggeredGrid` |\n\n\n ```kotlin\n // recyclerView.layoutManager = LinearLayoutManager(context)\n composeView.setContent {\n LazyColumn(Modifier.fillMaxSize()) {\n // We use a LazyColumn since the layout manager of the RecyclerView is a vertical LinearLayoutManager\n }\n }https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/interop/MigrationCommonScenariosSnippets.kt#L79-L84\n ```\n\n \u003cbr /\u003e\n\n3. Create a corresponding composable for each view type in your\n `RecyclerView.Adapter` implementation. Each view type typically maps to a\n `ViewHolder` subclass, though this may not always be the case. These\n composables will be used as the UI representation for different types of\n elements in your list:\n\n\n ```kotlin\n @Composable\n fun ListItem(data: MyData, modifier: Modifier = Modifier) {\n Row(modifier.fillMaxWidth()) {\n Text(text = data.name)\n // ... other composables required for displaying `data`\n }\n }https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/interop/MigrationCommonScenariosSnippets.kt#L124-L130\n ```\n\n \u003cbr /\u003e\n\n The logic in your `RecyclerView.Adapter`'s `onCreateViewHolder()` and\n `onBindViewHolder()` methods will be replaced by these composables and the\n state that you provide them with. In Compose, there is no separation between\n creating a composable for an item and binding data into it---these concepts are\n coalesced.\n4. Within the `content` slot of the Lazy list (the trailing lambda parameter),\n use the `items()` function (or an equivalent overload) to iterate through the\n data for your list. In the `itemContent` lambda, invoke the appropriate\n composable item for your data:\n\n\n ```kotlin\n val data = listOf\u003cMyData\u003e(/* ... */)\n composeView.setContent {\n LazyColumn(Modifier.fillMaxSize()) {\n items(data) {\n ListItem(it)\n }\n }\n }https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/interop/MigrationCommonScenariosSnippets.kt#L90-L97\n ```\n\n \u003cbr /\u003e\n\n| **Tip:** Provide additional parameters to `items()` to optimize your list: use the `key` parameter to provide a unique key for the underlying data so that scroll position will be maintained when items change, or use the `contentType` parameter to specify a content type for the underlying data (this is a similar concept to `RecyclerView`'s view types) so you can reuse item compositions more efficiently.\n\nCommon use cases\n\nItem decorations\n\n`RecyclerView` has the concept of an `ItemDecoration`, which you can use to add a\nspecial drawing for items in the list. For example, you can add an\n`ItemDecoration` to add dividers between items:\n\n\n```kotlin\nval itemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)\nrecyclerView.addItemDecoration(itemDecoration)https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/interop/MigrationCommonScenariosSnippets.kt#L103-L104\n```\n\n\u003cbr /\u003e\n\nCompose does not have an equivalent concept of item decorations. Instead, you\ncan add any UI decorations in the list directly in the composition. For example,\nto add dividers to the list, you can use the `Divider` composable after each\nitem:\n\n\n```kotlin\nLazyColumn(Modifier.fillMaxSize()) {\n itemsIndexed(data) { index, d -\u003e\n ListItem(d)\n if (index != data.size - 1) {\n HorizontalDivider()\n }\n }\n}https://github.com/android/snippets/blob/7a0ebbee11495f628cf9d574f6b6069c2867232a/compose/snippets/src/main/java/com/example/compose/snippets/interop/MigrationCommonScenariosSnippets.kt#L111-L118\n```\n\n\u003cbr /\u003e\n\nItem animations\n\nAn `ItemAnimator` can be set on a `RecyclerView` to animate the appearance of\nitems as changes are made to the adapter. By default, `RecyclerView` uses\n[`DefaultItemAnimator`](/reference/androidx/recyclerview/widget/DefaultItemAnimator) which provides basic animations on remove, add, and\nmove events.\n\nLazy lists have a similar concept through the `animateItemPlacement` modifier.\nSee [Item animations](/develop/ui/compose/lists#item-animations) to learn more.\n\nAdditional resources\n\nFor more information about migrating a `RecyclerView` to Compose, see the\nfollowing resources:\n\n- [Lists and Grids](/develop/ui/compose/lists#item-animations): Documentation for how to implement lists and grids in Compose.\n- [Jetpack Compose Interop: Using Compose in a RecyclerView](https://medium.com/androiddevelopers/jetpack-compose-interop-using-compose-in-a-recyclerview-569c7ec7a583): Blog post for efficiently using Compose within a `RecyclerView`.\n\nRecommended for you\n\n- Note: link text is displayed when JavaScript is off\n- [Lists and grids](/develop/ui/compose/lists)\n- [Migrate `CoordinatorLayout` to Compose](/develop/ui/compose/migrate/migration-scenarios/coordinator-layout)\n- [Other considerations](/develop/ui/compose/migrate/other-considerations)"]]