我如何檢測用戶何時從一個選項卡滑動到第二個選項卡等 HorizontalPager()?
val pagerState = rememberPagerState(initialPage = 0)
HorizontalPager(count = TabCategory.values().size, state = pagerState) { index ->
Box(
modifier = Modifier
.fillMaxSize()
.background(MaterialTheme.colors.onBackground)
) {
when (TabCategory.values()[index]) {
TabCategory.Opinion -> { }
TabCategory.Information -> { }
TabCategory.Videos -> { }
}
}
}
uj5u.com熱心網友回復:
在您的視圖模型中,創建一個 pagerState 并監視其 currentPage:
class MyViewModel : ViewModel() {
val pagerState = PagerState()
init {
viewModelScope.launch {
snapshotFlow { pagerState.currentPage }.collect { page ->
// Page is the index of the page being swiped.
}
}
}
}
在你的可組合中,使用 pagerState:
HorizontalPager(
state = myViewModel.pagerState,
) { page ->
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/qita/366969.html
