目录
Modifier.clickable() 取消点击水波纹
modifier = Modifier
.clickable(onClick = {}, indication = null, interactionSource = remember {
MutableInteractionSource()
})
LazyColum 相关
根据List.size设置内容,并提item的内容和position
LazyColumn {
itemsIndexed(viewState.localList) { position, local ->
LocalMusicItem(local, position) {
Log.d("TAG", "LocalScreen: $it")
}
}
}
LazyColum 取消overscroll效果(光晕)
CompositionLocalProvider(
LocalOverScrollConfiguration provides null //禁用
) {
LazyColumn() {
items() {
}
}
}
LazyColum 更新某项item State
//在ViewModel中
data class ItemState(
val selected: Boolean
val dataBean: DataBean
)
class ViewModel: ViewModel() {
val listState = mutableStateListOf<ItemState>()
fun selected(position: Int, isSelected: Boolean) {
items[position] = items[position].copy(
selected = isSelected
)
}
}
//在Compose中
val listState = viewModel.listState
LazyColumn {
//更新指定的某一项需要position索引,所以用itemsIndexed
itemsIndexed(listState) { position, itemState ->
item(
dataBean = itemState.dataBean,
isSelected = itemState.selected
onItemSelect = { pos, bean ->
viewModel.selected(pos, !itemState.selected)
}
)
}
}
如果要实现只有一项item选中效果,我目前使用的方法是
用一个int值保存上一个选中的item,点击的时候取消上一个item的选中状态,再把上一个选中item的值替换为当前item 的 position
贴张效果图
获取屏幕宽高
val configuration = LocalConfiguration.current
val screenHeightDp = configuration.screenHeightDp.dp
val screenHeightPx = with(LocalDensity.current) {
screenHeightDp.toPx()
}