java 使用lazycolumn喷气背包撰写滞后超过使用recycleview

y4ekin9u  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(86)

我刚刚学会了喷气背包作曲,我从回收到懒惰列,但当我这样做的时候,它滞后得比回收多
在向应用程序添加了大约100个项目后,应用程序会滞后,有什么方法可以减少懒惰列的滞后吗?我已经设置了项目的ID,并使用R8构建了以下代码:https://github.com/trongan123/TODOAPP

val tabs: MutableList<TabItem> = ArrayList<TabItem>().toMutableList()
tabs += TabItem("All") {
    AllItemScreen(
        listAll, openUpdateItemScreen = {
            openUpdateItemScreen()
        }, viewModel = viewModel
    )
}
tabs += TabItem("Pending") {
    PendingItemScreen(listAll.filter { it.status.equals("pending") }, openUpdateItemScreen = {
        openUpdateItemScreen()
    }, viewModel = viewModel)
}
tabs += TabItem("Completed") {
    CompletedItemScreen(
        listAll.filter { it.status.equals("completed") },
        openUpdateItemScreen = {
            openUpdateItemScreen()
        }, viewModel = viewModel
    )
}
val pagerState = rememberPagerState()
Scaffold(
    floatingActionButton = {
        FloatingActionButton(
            onClick = { openAddItemScreen() }
        ) {
            Icon(Icons.Filled.Add, "")
        }
    }
) { padding ->
    val textState = remember { mutableStateOf(TextFieldValue("")) }
    Column(
        modifier = Modifier
            .padding(padding)
    ) {
        SearchView(state = textState, viewModel)
        Spacer(modifier = Modifier.size(16.dp))
        Tabs(
            tabs = tabs, pagerState = pagerState,
            viewModel = viewModel
        )
        TabsContent(tabs = tabs, pagerState = pagerState)
    }
}

AllItemScreen

Column(
    modifier = Modifier
        .fillMaxSize()

) {
    LazyColumn() {

        items(listAll) { i ->
            ItemList(
                i,
                openUpdateItemScreen = {
                    openUpdateItemScreen()
                }, viewModel
            )
        }
    }
}

项目列表

val isChecked = remember { mutableStateOf(false) }
Card(
    modifier = Modifier
        .fillMaxWidth()
        .padding(5.dp)
        .clickable {
            if (isChecked.value == true) {
                isChecked.value = false
                viewModel.setClearAll(i.id, false)
                viewModel.setCheckData(i.id, false)
            } else {
                isChecked.value = true
                viewModel.setClearAll(i.id, true)
                viewModel.setCheckData(i.id, true)
            }
        },
    elevation = 5.dp
) {
    Column(
        modifier = Modifier.fillMaxSize(),
        verticalArrangement = Arrangement.Center
    ) {
        Spacer(modifier = Modifier.size(16.dp))
        Row {
            val check: List<Int>? = viewModel.getListMutableCheck().value as List<Int>
            if (check != null) {
                isChecked.value = check.contains(i.id)
            }
            Checkbox(
                checked = isChecked.value,
                onCheckedChange = {
                    isChecked.value = it
                    viewModel.setClearAll(i.id, it)
                    viewModel.setCheckData(i.id, it)
                }
            )
            Spacer(modifier = Modifier.size(16.dp))
            if (!isChecked.value) {
                Text(
                    i.title, modifier = Modifier
                        .padding(top = 10.dp)
                )
            } else Text(
                i.title,
                modifier = Modifier
                    .padding(top = 10.dp),
                style = TextStyle(textDecoration = TextDecoration.LineThrough)
            )
            Spacer(
                Modifier
                    .weight(1f)
                    .fillMaxHeight()
            )
            Image(painter = painterResource(id = R.drawable.ellipsis),
                contentDescription = null,

                modifier = Modifier
                    .clickable {
                        todoItem = i
                        openUpdateItemScreen()
                    }
                    .size(30.dp))
            Spacer(modifier = Modifier.size(10.dp))
        }

    }
}
xeufq47z

xeufq47z1#

Jetpack Compose在Debug模式下做了很多事情。你可以简单地在发布模式下测试你的代码,看看Jetpack Compose的威力

相关问题