Migrate RecyclerView to Lazy list
source link: https://developer.android.com/jetpack/compose/migrate/migration-scenarios/recycler-view
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
RecyclerView
is a View component that makes it easy to efficiently display
large sets of data. Instead of creating views for each item in the data set,
RecyclerView
improves the performance of your app by keeping a small pool of
views and recycling through them as you scroll through those items.
In Compose, you can use Lazy lists to accomplish the same thing. This page
describes how you can migrate your RecyclerView
implementation to use Lazy lists
in Compose.
Migration steps
To migrate your RecyclerView
implementation to Compose, follow these steps:
Comment out or remove the
RecyclerView
from your UI hierarchy and add aComposeView
to replace it if none is present in the hierarchy yet. This is the container for the Lazy list that you'll add:<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- <androidx.recyclerview.widget.RecyclerView-->
<!-- android:id="@+id/recycler_view"-->
<!-- android:layout_width="match_parent"-->
<!-- android:layout_height="match_parent />"-->
<androidx.compose.ui.platform.ComposeView
android:id="@+id/compose_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</FrameLayout>Determine what type of Lazy list composable you need based on your
RecyclerView
’s layout manager (see table below). The composable you select will be the top-level composable of theComposeView
you added in the previous step.LayoutManager
Composable
LinearLayoutManager
LazyColumn
orLazyRow
GridLayoutManager
LazyVerticalGrid
orLazyHorizontalGrid
StaggeredGridLayoutManager
LazyVerticalStaggeredGrid
orLazyHorizontalStaggeredGrid
// recyclerView.layoutManager = LinearLayoutManager(context)
composeView.setContent {
LazyColumn(Modifier.fillMaxSize()) {
// We use a LazyColumn since the layout manager of the RecyclerView is a vertical LinearLayoutManager
}
}Create a corresponding composable for each view type in your
RecyclerView.Adapter
implementation. Each view type typically maps to aViewHolder
subclass, though this may not always be the case. These composables will be used as the UI representation for different types of elements in your list:@Composable
fun ListItem(data: MyData, modifier: Modifier = Modifier) {
Row(modifier.fillMaxWidth()) {
Text(text = data.name)
// … other composables required for displaying `data`
}
}The logic in your
RecyclerView.Adapter
’sonCreateViewHolder()
andonBindViewHolder()
methods will be replaced by these composables and the state that you provide them with. In Compose, there is no separation between creating a composable for an item and binding data into it—these concepts are coalesced.Within the
content
slot of the Lazy list (the trailing lambda parameter), use theitems()
function (or an equivalent overload) to iterate through the data for your list. In theitemContent
lambda, invoke the appropriate composable item for your data:val data = listOf<MyData>(/* ... */)
composeView.setContent {
LazyColumn(Modifier.fillMaxSize()) {
items(data) {
ListItem(it)
}
}
}
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.
Common use cases
Item decorations
RecyclerView
has the concept of an ItemDecoration
, which you can use to add a
special drawing for items in the list. For example, you can add an
ItemDecoration
to add dividers between items:
val itemDecoration = DividerItemDecoration(recyclerView.context, LinearLayoutManager.VERTICAL)
recyclerView.addItemDecoration(itemDecoration)
Compose does not have an equivalent concept of item decorations. Instead, you
can add any UI decorations in the list directly in the composition. For example,
to add dividers to the list, you can use the Divider
composable after each
item:
LazyColumn(Modifier.fillMaxSize()) {
itemsIndexed(data) { index, d ->
ListItem(d)
if (index != data.size - 1) {
Divider()
}
}
}
Item animations
An ItemAnimator
can be set on a RecyclerView
to animate the appearance of
items as changes are made to the adapter. By default, RecyclerView
uses
DefaultItemAnimator
which provides basic animations on remove, add, and
move events.
Lazy lists have a similar concept through the animateItemPlacement
modifier.
See Item animations to learn more.
Additional resources
For more information about migrating a RecyclerView
to Compose, see the
following resources:
- Lists and Grids: Documentation for how to implement lists and grids in Compose.
- Jetpack Compose Interop: Using Compose in a RecyclerView:
Blog post for efficiently using Compose within a
RecyclerView
.
Recommended for you
Lists and grids
Jetpack Compose is Android's recommended modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.
Migrate CoordinatorLayout to Compose
Jetpack Compose is Android's recommended modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.
Other considerations
Jetpack Compose is Android's recommended modern toolkit for building native UI. It simplifies and accelerates UI development on Android. Quickly bring your app to life with less code, powerful tools, and intuitive Kotlin APIs.
Recommend
-
351
Android开发:RecyclerView平滑流畅的滑动到指定位置 2017年10月17日 12:14 · 阅读 12105 ...
-
85
Attention. This project is not maintained any more !!! vlayout 中文文档 Projects of Tangram Android Project
-
107
AutoAdapter This Repository simplifies working with RecyclerView Adapter Gradle: Add it in your root build.gradle at the end of repositories: allprojects { repositories { ... maven { url 'https://ji...
-
87
RecyclerViewCardGallery 声明:代码fork自 https://github.com/huazhiyuan2008/RecyclerViewCardGallery/ 感谢原作者 RecyclerView实现循环ba...
-
98
用RecyclerView打造一个轮播图(进阶版) - 掘金 用RecyclerView打造一个轮播图(进阶版) 2017年11月22日 10:02 ...
-
144
支持粘性Item的RecyclerView布局管理器 2017年12月05日 02:18 · 阅读 3110 很...
-
72
RecyclerBanner RecyclerBanner是一个利用RecycleView实现轮播图的自定义控件。 这里有相关博客介绍!。 属性名称
-
115
用RecyclerView做一个小清新的Gallery效果 2017年12月13日 10:18 · 阅读 12244
-
148
Epoxy Epoxy is an Android library for building complex screens in a RecyclerView. Models are automatically generated from custom views or databinding layouts via annotation processing. These models are then used in an EpoxyController to d...
-
91
2018年01月01日 阅读 4114 Android 从零开始实现RecyclerView分组及粘性头部效果 版权声明:本文为博主原创文...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK