0

我正在使用 LazyColumn 可组合来实现带有粘性标题的折叠工具栏行为。然后我意识到 Lazycolumn 会不断地渲染项目并且从未停止这样做。作为一个例子,下面的代码(不能比这更容易)会给我下面的logcat:

@Composable
fun Scanner() {


    val listState = rememberLazyListState()
    val list = listOf(0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20)

    LazyColumn(
        modifier = Modifier
            .background(MaterialTheme.colors.background)
            .wrapContentHeight()
            .fillMaxWidth(),
        state = listState,
        contentPadding = PaddingValues(start = 1.dp, end = 1.dp, bottom = 1.dp),
        verticalArrangement = Arrangement.spacedBy(8.dp),
        ) {

        itemsIndexed(list){index, item ->

            key(index){

                Card(
                    modifier = Modifier
                        .padding(5.dp)
                        .fillMaxWidth()
                        .height(50.dp),
                    elevation = 0.dp,

                    ) {

                    Log.d("Lazy ColumScanner", "Composing:$item ")

                    Text(
                        text = "$item",
                        fontSize = 25.sp
                    )
                  
                }
            }
        }
    }
}

2021-07-13 19:29:15.025 14066-14072/com.paulhugon.digipaule I/zygote: Increasing code cache capacity to 512KB
2021-07-13 19:29:15.123 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.153 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.174 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.199 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.222 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.244 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.264 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.283 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.456 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.461 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.468 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.473 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.478 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.485 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.491 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.498 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.567 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.573 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.579 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.585 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.589 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.594 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.599 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.604 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.605 14066-14072/com.paulhugon.digipaule I/zygote: Do full code cache collection, code=252KB, data=221KB
2021-07-13 19:29:15.607 14066-14072/com.paulhugon.digipaule I/zygote: After code cache collection, code=221KB, data=165KB
2021-07-13 19:29:15.646 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.650 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.653 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.657 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.661 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.664 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.668 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.672 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.717 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.722 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.726 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.730 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.734 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.737 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.741 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.745 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.793 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.797 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.802 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.806 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.810 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.814 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.818 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.823 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 
2021-07-13 19:29:15.870 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:0 
2021-07-13 19:29:15.874 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:1 
2021-07-13 19:29:15.878 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:2 
2021-07-13 19:29:15.883 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:3 
2021-07-13 19:29:15.886 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:4 
2021-07-13 19:29:15.891 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:5 
2021-07-13 19:29:15.896 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:6 
2021-07-13 19:29:15.899 14066-14066/com.paulhugon.digipaule D/Lazy ColumScanner: Composing:7 

为了问题的长度,我剪掉了 logcat,但只要应用程序处于打开状态,它就会继续运行......这是在 jetpack-compose 版本 beta09 上。这是一种正常的行为还是正常的,我觉得 LazyColumn 内的可组合物无限期地(重新)组合起来很奇怪?希望有人可以就此事分享一些启示。谢谢你的时间

4

0 回答 0