![](/img/trans.png)
[英]How to disable and enable scrolling in LazyColumn/LazyRow in Jetpack Compose?
[英]How to disable scrolling in LazyColumn and Column
我想在我的 LazyColumn 或 Column 中禁用滾動。
Modifier.scrollable(state = rememberScrollState(), enabled = false, orientation = Orientation.Vertical)
要么
Modifier.verticalScroll(...)
不工作。
這是我的代碼:
Column(
modifier = Modifier
.fillMaxSize()
) {
Box(
modifier = Modifier
.padding(15.dp)
.height(60.dp)
.clip(RoundedCornerShape(30))
) {
TitleSection(text = stringResource(id = R.string...))
}
LazyColumn(
contentPadding = PaddingValues(start = 7.5.dp, end = 7.5.dp, bottom = 100.dp),
modifier = Modifier
.fillMaxHeight()
) {
items(categoryItemContents.size) { items ->
CategoryItem(categoryItemContents[items], navController = navController)
}
}
}
一種簡單的方法是將 LazyColumn 放在包含另一個 Box 的 Box 中。 可以組合嵌套的 Box 來攔截滾動,從而防止 LazyColumn 接收任何滾動事件。 要啟用滾動,只需阻止添加嵌套的 Box。 至於禁用列中的滾動,這是默認設置。 默認情況下,列沒有滾動:
class MainActivity : ComponentActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
startActivity(intent)
setContent {
var scrollingEnabled by remember { mutableStateOf(true) }
Column() {
Row(verticalAlignment = Alignment.CenterVertically) {
Text("Scrolling Enabled")
Switch(
checked = scrollingEnabled,
onCheckedChange = { scrollingEnabled = it }
)
}
Box(modifier = Modifier.fillMaxSize()) {
LazyColumn(Modifier.fillMaxWidth(), state = rememberLazyListState()) {
items((1..100).toList()) {
Text("$it")
}
}
if (!scrollingEnabled) {
Box(modifier = Modifier.fillMaxSize().verticalScroll(rememberScrollState())) {}
}
}
}
}
}
}
使用LazyColumn
,您可以使用userScrollEnabled
參數來禁用滾動。
LazyColumn(
state = state,
userScrollEnabled = false
){
//items
}
請注意,您仍然可以使用 state 以編程方式滾動,即使它已被禁用。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.