Android Jetpack Compose Row 的高度 (IntrinsicSize.Min) 在子列生成更多可组合项时不会拉伸



下面是我的代码片段。我传入editClick,它将一个数据类对象添加到chargingViewModel.contractSelfPay中,它被观察为itemList状态。当我点击图标时,我可以告诉itemList状态通过拥有更多均匀间隔的编辑图标来接收更新。但是,BasicGrid行的高度没有使用Intrinsic.Min.进行拉伸

如果我删除IntrinsicSize.Min,即使行的高度被拉伸,分隔符也不能填充MaxHeight和图标列。无内在最小

@Composable
fun ContractSelfPay(chargingViewModel: ChargingViewModel, editClick: () -> Unit = {}) {
val itemList by chargingViewModel.contractSelfPay.observeAsState()
val composeList: List<@Composable () -> Unit> = itemList?.map {
@Composable {
Row {
TempFunc { StyledText(text = it.itemTitle) }
TempFunc { StyledText(text = it.originalPrice.toString()) }
TempFunc { StyledText(text = it.selfPay.toString(), color = self_pay_blue) }
TempFunc { StyledText(text = it.count.toString()) }
TempFunc { StyledText(text = (it.selfPay * it.count).toString()) }
}
}
} ?: listOf()
val total = itemList?.map { (it.selfPay.toInt() * it.count.toInt()) }?.sum() ?: 0
BasicGrid("全自費", composeList, total = total.toString(), editClick = editClick)
}
@Composable
fun BasicGrid(
gridTitle: String,
itemList: List<@Composable () -> Unit>,
total: String = "0",
editClick: () -> Unit = {}
) {
Row(modifier = Modifier.height(IntrinsicSize.Min), verticalAlignment = Alignment.CenterVertically) {
StyledTextBold(text = gridTitle, modifier = Modifier.weight(15f).wrapContentWidth())
VerticalDivider()
Column(
modifier = Modifier.weight(60f)
) {
itemList.forEachIndexed { index, compose ->
compose()
if (index != itemList.size - 1)
HorizontalDivider()
}
if (itemList.isEmpty())
StyledText(text = "尚未有任何紀錄", modifier = Modifier.weight(1f).wrapContentSize())
}

VerticalDivider()
StyledTextBold(text = total, modifier = Modifier.weight(15f).wrapContentWidth())
VerticalDivider()
Column(
modifier = Modifier
.weight(10f)
.fillMaxHeight(),
verticalArrangement = Arrangement.SpaceEvenly
) {
itemList.forEachIndexed { index, detail ->
Image(
painter = painterResource(R.drawable.icon_mode_edit),
contentDescription = "",
modifier = Modifier
.align(Alignment.CenterHorizontally)
.clickable { editClick() },
)
if (itemList.isNotEmpty() && index != itemList.size - 1)
HorizontalDivider()
}
}
}
}

我在这里创建了问题https://issuetracker.google.com/issues/217910352.希望它能得到解决。

我能想到的解决办法之一是跟踪高度并删除IntrinsicSize.Min.

如:

// _key_ is something that causes change of the height of the row
var height by remember(_key_) { mutableStateOf(0) } 
Row(Modifier.onSizeChanged { height = it.height }) {
VerticalDivider(Modifier.height(height))
}

在您的情况下,我认为将是itemList的大小。

谢谢Majkee。已经有一段时间了。我当时修复它的方法是使用自定义布局修改器。但不确定它今天是否仍然有效。

fun Modifier.expandHeight() = this.then(layout { measurable, constraints ->
val placeable =
measurable.measure(constraints.copy(maxHeight = Constraints.Infinity))
layout(placeable.width, placeable.height) {
placeable.placeRelative(0, 0)
}
})

使用它可以进行

Column(modifier = Modifier.expandHeight())

最新更新