https://kotlinlang.org logo
#compose
Title
# compose
g

Gerardo Ernesto Rodriguez Navar

01/20/2022, 2:38 PM
Does anyone know how to update a recyclerview that is inside a Compose AndroidView?
This is the AndroidView that contains the recyclerview
Copy code
AndroidView(
    factory = { context ->
        val recyclerView = RecyclerView(context)
        val linearLayoutManager =
            LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false)

        with(recyclerView) {
            layoutManager = linearLayoutManager
            adapter = dateSelectorAdapter
        }

        recyclerView
    },
    update = {}
)
When a
mutableState
changes in my
viewModel
ideally I would want to update the
adapter
to update the
recyclerView
z

Zach Klippenstein (he/him) [MOD]

01/20/2022, 2:59 PM
Move your
adapter = dateSelectorAdapter
to the
update
lambda
g

Gerardo Ernesto Rodriguez Navar

01/20/2022, 3:06 PM
Thanks @Zach Klippenstein (he/him) [MOD]! I’ll try it 😄
c

Casey Brooks

01/20/2022, 3:19 PM
The documentation of AndroidView) may be helpful, there are a few important things to know about it, and it has guidance on how to use it properly. The docs explain that
factory
only produces the view and will only be called once, while "the update block can be run multiple times due to recomposition, and it is the right place to set View properties depending on state"
👌 1
☝🏻 1