yousefa2
03/09/2021, 9:56 AMAndroidView
. The View
used for AndroidView
is provided by a generic provider that takes in some input and outputs a View
type.
@Composable
fun BasketItemsList(
modifier: Modifier,
basketItems: List<ProductState>,
provider: ProductCard,
interactionListener: ProductCardView.InteractionListener
) {
LazyColumn(modifier = modifier) {
items(basketItems) { product ->
AndroidView(factory = { provider.view }) {
Timber.tag("BasketItemsList").v(product.toString())
provider.setup(product, interactionListener)
}
}
}
}
With this sample, any interaction with the ProductCard view’s doesn’t update the screen. Logging shows that the state gets updated but the catch is that it’s only updated once per button. For example, I have a favourites button. Clicking it once pushes a state update only once, any subsequent clicks doesn’t propagate. Also the card itself doesn’t updates. It’s as if it was never clicked.
Now changing the block of AndroidView.update
to cast from the ProductCard
interface to a concrete view type works as expected. All clicks propagate correctly and the card view gets updated to reflect the state.
@Composable
fun BasketItemsList(
modifier: Modifier,
basketItems: List<ProductState>,
provider: ProductCard,
interactionListener: ProductCardView.InteractionListener
) {
LazyColumn(modifier = modifier) {
items(basketItems) { product ->
AndroidView(factory = { provider.view }) {
Timber.tag("BasketItemsList").v(product.toString())
// provider.setup(product, interactionListener)
(it as ProductCardView).setup(product, interactionListener)
}
}
}
}
What am I missing here? why does using ProductCard
type not working while casting the view to it’s type is working?Zach Klippenstein (he/him) [MOD]
03/09/2021, 4:57 PMprovider.setup
, in the second you’re calling it.setup
. In the first case it looks like you’re calling the method on the wrong object.yousefa2
03/10/2021, 1:00 PMprovider
is a view that implements ProductCard
. Something like
class ProductCardView: View(), ProductCardView {
override val view: View = this
override fun setup(...) {}
}
it as ProductCard
also works. So the key is using the it
that’s passed through AndroidView.update
but they are the same object so not sure why it wouldn’t work.Zach Klippenstein (he/him) [MOD]
03/10/2021, 3:29 PMyousefa2
03/10/2021, 5:58 PM