我想以编程方式动态启用和禁用滚动LazyColumn
.
好像没有相关功能LazyListState
或相关参数LazyColumn
本身。我怎样才能在 Compose 中实现这一目标?
Since 1.2.0 userScrollEnabled
范围加入 https://developer.android.com/jetpack/androidx/releases/compose-foundation#1.2.0-alpha01 to LazyColumn
, LazyRow
, and LazyVerticalGrid
LazyColumn(userScrollEnabled = false) ...
回答1.1.0和早期版本:
@Ryan 的解决方案还将禁用以编程方式调用的滚动。
这是维护者提出的解决方案此功能请求 https://issuetracker.google.com/issues/201150093。它将禁用滚动,允许编程滚动以及子视图触摸。
private val VerticalScrollConsumer = object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource) = available.copy(x = 0f)
override suspend fun onPreFling(available: Velocity) = available.copy(x = 0f)
}
private val HorizontalScrollConsumer = object : NestedScrollConnection {
override fun onPreScroll(available: Offset, source: NestedScrollSource) = available.copy(y = 0f)
override suspend fun onPreFling(available: Velocity) = available.copy(y = 0f)
}
fun Modifier.disabledVerticalPointerInputScroll(disabled: Boolean = true) =
if (disabled) this.nestedScroll(VerticalScrollConsumer) else this
fun Modifier.disabledHorizontalPointerInputScroll(disabled: Boolean = true) =
if (disabled) this.nestedScroll(HorizontalScrollConsumer) else this
Usage:
LazyColumn(
modifier = Modifier.disabledVerticalPointerInputScroll()
) {
// ...
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)