简体   繁体   English

为什么我的 LazyColumn 的重组不会触发

[英]Why won't the recomposition trigger for my LazyColumn

I have a LazyColumn which has multiple lists which it's supposed to display depending on the index value.我有一个 LazyColumn ,它有多个列表,它应该根据index值显示。 However, when I change the index the list changes, but the items do not get redrawn until I scroll down and back up.但是,当我更改index时,列表会发生变化,但在我向下滚动和备份之前,这些项目不会被重绘。 I've tossed around the remember keyword, changed my logic N times and it still won't update.我已经折腾了 remember 关键字,改变了我的逻辑 N 次,它仍然不会更新。 Here are my classes这是我的课

    @Composable
fun MainContent() {
    val state = homeViewModel.state.collectAsState(initial = HomepageState.Loading)
    Theme(config = config) {
        when (state.value) {
            is HomepageState.Loading -> Box(
                modifier = Modifier.fillMaxSize(),
                contentAlignment = Alignment.Center
            ) { CircularProgressIndicator() }
            is HomepageState.Multi -> with(state.value as HomepageState.Multi) {
                updateHomepageImagePreference(index)
                LazyColumnContent(homepage = items, switcher, logo, index)
            }
        }
    }
}

The homepage[index] part is the one I would like to trigger recomposition. homepage[index] 部分是我想触发重组的部分。 I've tried to pass the correct list instead of changing the index, but the result is the same我试图传递正确的列表而不是更改索引,但结果是一样的

@Composable
private fun LazyColumnContent(
    homepage: List<List<ModuleConfig>>,
    switcher: HomepageSwitcherTheme?,
    logo: HomepageThemeNavLogo?,
    index: Int = 0
) {
    LaunchedEffect(key1 = index) {
        updateHomepageSwitcher(switcher)
        updateNavigationBarLogo(logo)
    }
    return LazyColumn(
        modifier = Modifier
            .fillMaxSize()
            .background(vennConfig.themeConfig.backgroundColor?.color)
    ) {
        itemsIndexed(homepage[index]) { _, item ->
            AndroidView(
                modifier = Modifier.fillMaxSize(),
                factory = {
                    val productsInCategoryCriteriaSatisfied =
                        if (item.requiresProductsInCategoryId.isNullOrEmpty()) true
                        else categoryHasProducts[item.requiresProductsInCategoryId] ?: true

                    return@AndroidView if (productsInCategoryCriteriaSatisfied) moduleFactory.build(
                        item,
                        requireContext()
                    )
                    else View(context) // blank view
                }
            )
        }
    }
}

I'm guessing I'm doing something wrong with my Compose usage, but I can't figure out what.我猜我的 Compose 用法有问题,但我不知道是什么。

AndroidView factory is only getting called once the view appears.只有在视图出现后才会调用AndroidView factory If you need to update item on the same view, you can use update .如果您需要更新同一视图上的item ,可以使用update Also you don't need to create an empty view when you have nothing to show, just create AndroidView on demand:此外,当您没有可显示的内容时,您无需创建空视图,只需按需创建AndroidView

val productsInCategoryCriteriaSatisfied =
    if (item.requiresProductsInCategoryId.isNullOrEmpty()) true
    else categoryHasProducts[item.requiresProductsInCategoryId] ?: true

if (productsInCategoryCriteriaSatisfied) {
    AndroidView(
        modifier = Modifier.fillMaxSize(),
        factory = { context ->
            moduleFactory.build(
                item,
                context
            )
        },
        update = {
            it.item = item
        },
    )
}

An other problem is that you may have change the items order.另一个问题是您可能更改了项目顺序。

The cleanest way of solving this problem is using key parameter: by default it's equal item index.解决这个问题的最简洁的方法是使用key参数:默认情况下它是相等的项目索引。

Perfectly your item have an id , but also you can build the key depending on any other item properties.完美地,您的项目有一个id ,但您也可以根据任何其他项目属性构建key When the key is not the same since the last recomposition - AndroidView will be re-created.当自上次重组后key不同时 - 将重新创建AndroidView ps when you don't need index, you can use items instead of itemsIndexed : ps 当你不需要索引时,你可以使用items而不是itemsIndexed

LazyColumn {
    items(list, key = { it.id }) { item ->

    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM