从Jetpack Compose的LazyColumn中删除项目的正确方法

huangapple go评论58阅读模式
英文:

Correct way to remove item from the LazyColumn in jetpack compose

问题

I want to remove item from the LazyColumn. I have a bunch of item in the list i.e. 200+ item. When I removed item it little bit slow. I don't understand what is the problem in my code. I am adding basic sample what I did try on my code.

RemoveItem

@Composable
fun RemoveItem(viewModel: RemovedItemViewModel) {
    var newList = viewModel.itemList
    LaunchedEffect(key1 = viewModel.itemList) {
        newList = viewModel.itemList
    }

    LazyColumn {
        items(newList) {
            Text(text = "$it")
        }
    }
}

RemovedItemViewModel

class RemovedItemViewModel : BaseViewModel() {
    val itemList = mutableStateListOf<MovieItem>()

    init {
        repeat(10) {
            itemList.add(MovieItem("$it cast", "Movie $it"))
        }
    }

    fun removeItem() {
        val random = (0..10).random()
        itemList.removeAt(random)
    }
}

MovieItem

data class MovieItem(val number: String, val name: String)

Is there any better way to remove item?

英文:

I want to remove item from the LazyColumn. I have a bunch of item in the list i.e. 200+ item. When I removed item it little bit slow. I don't understand what is the problem in my code. I am adding basic sample what I did try on my code.

RemoveItem

@Composable
fun RemoveItem(viewModel: RemovedItemViewModel) {
    var newList = viewModel.itemList
    LaunchedEffect(key1 = viewModel.itemList) {
        newList = viewModel.itemList
    }

    LazyColumn {
        items(newList) {
            Text(text = &quot;$it&quot;)
        }
    }
}

RemovedItemViewModel

class RemovedItemViewModel : BaseViewModel() {
    val itemList = mutableStateListOf&lt;MovieItem&gt;()

    init {
        repeat(10) {
            itemList.add(MovieItem(&quot;$it cast&quot;, &quot;Movie $it&quot;))
        }
    }

    fun removeItem() {
        val random = (0..10).random()
        itemList.removeAt(random)
    }
}

MovieItem

data class MovieItem(val number: String, val name: String)

Is there any better way to remove item?

答案1

得分: 1

以下是翻译好的内容:

你可能会出现性能问题,有几个原因造成这种情况。

第一个原因是你在使用项目时没有提供唯一的键。为了使列表项目操作更加优化,你需要使用唯一的键。如果不唯一,你将会遇到运行时异常,所以请小心。

第二个问题是不必要的LaunchedEffect,正如评论中已经指出的那样。

另外,为了确定你是否真的存在性能问题,我建议你首先在你的 build.gradle 文件中将 isDebuggable = false,然后使用启用了 R8 的发布版本进行检查。在使用Compose时,调试模式和发布模式之间存在很大差异,可以在这里了解更多信息。

如果尽管我上面列出的一切你仍然遇到性能问题,那么我建议你安装最新的 Android Studio Hedgehog 预览版本。在其中,你可以通过调试组合状态来查找为什么会发生不必要的重组。

英文:

You can have performance problems for several reasons.

The first reason is that you are using items without a key. In order for list item operations to be more optimized you need to use a unique key. If it is not unique, you will get a runtime exception, so be careful.

The second issue is the unnecessary LaunchedEffect as already noticed on the comments

Also, in order to determine if you really have problems with performance, I would recommend that you first to check your app with isDebuggable = false in your build.gradle file as well as the using release build with enabled R8. Here is a very big difference between debug and release modes when we're using compose, read more here.

If despite everything that I have listed above you still encounter performance problems, then I would recommend that you install a latest preview version of Android Studio Hedgehog. In it you can check why unnecessary recompositions happen by debugging the composition state

huangapple
  • 本文由 发表于 2023年6月1日 00:41:59
  • 转载请务必保留本文链接:https://go.coder-hub.com/76375686.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定