简体   繁体   English

LazyColumn 中的 LazyRow 每次都调用 recompose

[英]LazyRow inside LazyColumn call recompose every time

Hi I have an object with a list of other objects.嗨,我有一个带有其他对象列表的对象。 I need to show a list of the first one, and inside each object show a horizontal list with the other objects.我需要显示第一个列表,并在每个对象内显示一个包含其他对象的水平列表。

I have these object stored in local database by room.我将这些对象按房间存储在本地数据库中。 These objects are related with a cross ref:这些对象与交叉引用相关:

data class ObjectWithOtherObjectsCrossRef(
    @ColumnInfo(name = "object_id")
    val objectId: Long,
    @ColumnInfo(name = "other_object_id")
    val otherObjectId: Long,
)

I have a lazyRow inside a lazyColumn.我在lazyColumn 中有一个lazyRow。 The lazyColumn is populated by a room flow. lazyColumn 由房间流填充。 The first time I go to the screen, the view acts properly, but when I insert a new item, recompose is calling every time and the scroll became slow.我第一次进入屏幕时,视图动作正常,但是当我插入一个新项目时,每次都调用 recompose 并且滚动变慢。

Any idea about why recompose is calling every time?关于为什么 recompose 每次都调用的任何想法? Thanks!谢谢!

Here is my code:这是我的代码:

DataModel:数据模型:

data class MyObject(
    val id: Long,
    val name: String,
    val list: List<OtherObject>
)

data class OtherObject(
    val id: Long,
    val name: String
)

UiState:状态:

sealed class MyObjectState {
    object Loading: MyObjectState()
    data class Success(val data: List<MyObject>): MyObjectState()
}

ViewModel:视图模型:

val uiState: StateFlow<MyObjectState> = getAllObjects().stateIn(
    scope = viewModelScope,
    started = SharingStarted.WhileSubscribed(5_000),
    initialValue = MyObjectState.Loading
)

fun addNewObject(object: Long, otherObject: Long) {
    viewModelScope.launch {
        addNewObjectUseCase(object, otherObject)
    }
}

Composable可组合的

@Composable
fun MainView() {
    val state: MyObjectState by viewModel.uiState.collectAsState()

    when (state) {
        MyObjectState.Loading -> {...}
        is MyObjectState.Success -> ListScreen(state.data)
}



@Composable
fun ListScreen(
    val list: List<MyObject>
) {
    LazyColumn {
        items(
            items = list,
            key = { it.id }
        ) { 
            Text(text = it.name)
            LazyRow {
                items(
                    items = it.list,
                    key = { it.id },
                ) { 
                    Text(text = it.name)
                }
            }
        }
    }
}

I tried downgrading the compose version to the stable one (1.1.1) and it works fine.我尝试将 compose 版本降级为稳定版本(1.1.1),它工作正常。

I was using version 1.2.0.beta02我使用的是 1.2.0.beta02 版本

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

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