xxfast
10/27/2022, 11:50 PMRecyclerView within a @Composable like this
@Composable
fun ComposeRecyclerView(items: Items) {
val adapter: RecyclerView.Adapter by remember { mutableStateOf( RecyclerView.Adapter()) }
AndroidView(
modifier = Modifier.fillMaxSize(),
factory = { context -> RecyclerView(context) },
update = { recyclerView ->
if (recyclerView.adapter == null) {
recyclerView.adapter = adapter
recyclerView.adapter?.stateRestorationPolicy =
RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
}
adapter.set(animatedState.items)
}
)
}
This doesn't seem to work. Is this the right way to use a legacy RecyclerView within Compose? Google is failing me because it keep showing me LazyColumn APIs which is not what I'm trying to achieve here 😅czuckie
10/27/2022, 11:51 PMxxfast
10/27/2022, 11:53 PMxxfast
10/27/2022, 11:53 PMczuckie
10/28/2022, 12:00 AMBen Trengrove [G]
10/28/2022, 12:30 AMBen Trengrove [G]
10/28/2022, 12:32 AMfactory = { context -> RecyclerView(context).apply {
val myAdapter = RecyclerView.Adapter()
myAdapter..stateRestorationPolicy = RecyclerView.Adapter.StateRestorationPolicy.PREVENT_WHEN_EMPTY
adapter = myAdapter
}
Something like thatBen Trengrove [G]
10/28/2022, 12:32 AMxxfast
10/28/2022, 12:55 AMBen Trengrove [G]
10/28/2022, 5:22 AMmutableStateOf. You can just do remember { MyObject() }xxfast
10/29/2022, 1:45 AMState<T> (from remember { mutableStateOf() }) helps to trigger recomposition when its state changes. However, remember { YourObject } just prevents reallocation when the @Composable function is executed multiple timesBen Trengrove [G]
10/31/2022, 4:55 AMremember - caches objects across recompostion
mutableStateOf() - creates a new mutable state.
If you didn't remember your mutableStateOf, you would be creating a new mutable state on every recomposition