Android Jetpack Compose 列表使用重影图像重新组合

时间:2021-05-27 13:19:30

标签: android-jetpack-compose

我在重组列表时遇到了奇怪的行为。我有一个项目列表,当按下切换组时,项目的顺序会发生变化。关联图像也将根据项目而变化。奇怪的是,我得到了新订单和新的关联图像就好了,只是在订单更改之前从上一个项目中保留了一个“幽灵”图像。

之前:

enter image description here

行为后的当前:

enter image description here

行为后的预期:

enter image description here

相关代码:

@Composable
fun TestsView(dzlink: String, navController: NavController) {

    val testsViewModel = TestsViewModel(dzlink)
    val list : ArrayList<TestEntity> by testsViewModel.list.observeAsState(arrayListOf<TestEntity>())
    var arrangement by rememberSaveable { mutableStateOf(TestArrangement.abc) }

    when (arrangement) {
        TestArrangement.abc -> list.sortBy { it.name.toLowerCase() }
        TestArrangement.bin -> list.sortByDescending { it.lr_avg }
        TestArrangement.bout -> list.sortBy { it.nlr_avg }
    }

    LazyColumn(Modifier.fillMaxSize()) {
        item {
            SegmentedButton(checkedIdent = arrangement.value, onArrangementChange = { arrangement = it })
        }
        itemsIndexed(items = list, itemContent = { index, item ->
            MainRow(
                item,
                navController
            )
        })
    }
}


@Composable
private fun MainRow(item: TestEntity, navController: NavController) {
    Row(verticalAlignment = Alignment.CenterVertically,
        modifier = Modifier
            .clickable {

            }
            .fillMaxWidth()
            .defaultMinSize(minHeight = 64.dp)
            .padding(horizontal = 16.dp, vertical = 8.dp)
            .fillMaxSize()
    ) {
        Column {
            Text(item.name, fontWeight = FontWeight.SemiBold)
            AccuracyView(item, fontSize = 16)
        }

    }

}


@Composable
fun AccuracyView(test: TestEntity, fontSize: Int) {

    var pmod = test.lr_avg.pscale()*8f
    var nmod = test.nlr_avg.nscale()*8f

    Row {
        Text(buildAnnotatedString {
            if (!test.lr_avg.lrCheckNil()) {
                append("+LR " + test.lr_avg.round1dec())
                withStyle(style = SpanStyle(color = MaterialTheme.colors.primaryVariant)) {
                    append(" (" + test.lr_min.round1dec() + " - " + test.lr_max.round1dec() + ")")
                }
            } else {
                append("+LR N/A")
            }
            append(", ")
            if (!test.nlr_avg.lrCheckNil()) {
                append("-LR " + test.nlr_avg.round1dec())
                withStyle(style = SpanStyle(color = MaterialTheme.colors.primaryVariant)) {
                    append(" (" + test.nlr_min.round1dec() + " - " + test.nlr_max.round1dec() + ")")
                }
            } else {
                append("-LR N/A")
            }
        }, fontSize = fontSize.sp, modifier = Modifier.weight(9f))
        Spacer(Modifier.width(16.dp))
        Column(modifier = Modifier.width(48.dp).weight(1f)) {
            Row {
                Box(
                    modifier = Modifier.size(24.dp).weight(1f),
                    contentAlignment = Alignment.BottomCenter
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_up),
                        "contentDescription",
                        modifier = Modifier.height(pmod.dp),
                        tint = MaterialTheme.colors.primaryVariant
                    )
                }
                Box(
                    Modifier
                        .size(24.dp).weight(1f),
                    contentAlignment = Alignment.BottomCenter
                ) {
                    Icon(
                        painter = painterResource(id = R.drawable.ic_down),
                        "contentDescription",
                        modifier = Modifier.height(nmod.dp),
                        tint = MaterialTheme.colors.primaryVariant
                    )
                }
            }
            Divider(thickness = 1.dp, modifier = Modifier.width(48.dp))
        }
    }
}

0 个答案:

没有答案