Eab*_*ryt 6 android pager kotlin android-jetpack-compose jetpack-compose-accompanist
我对喷气背包还很陌生,并且正在遵循本指南。
当尝试创建我的选项卡时,我点击了一个UnresolvedReference,我不知道为什么
@ExperimentalPagerApi
@ExperimentalMaterialApi
@Composable
fun Tabs(tabs: List<TabItem>, pagerState: PagerState) {
val scope = rememberCoroutineScope()
TabRow(
selectedTabIndex = pagerState.currentPage,
backgroundColor = colorResource(id = R.color.colorPrimaryDark),
contentColor = Color.White,
indicator = { tabPositions ->
TabRowDefaults.Indicator(
Modifier.pagerTabIndicatorOffset(pagerState, tabPositions)
)
}) {
tabs.forEachIndexed { index, tab ->
LeadingIconTab(
selected = pagerState.currentPage == index,
text = { Text(tab.title) },
icon = { Icon(painter = painterResource(id = tab.icon), contentDescription = "") },
onClick = {
scope.launch {
pagerState.animateScrollToPage(index)
}
},
)
}
}
Run Code Online (Sandbox Code Playgroud)
其他一切都有效。我有implementation("com.google.accompanist:accompanist-pager:0.28.0")并且build.gradle正在导入
我是否在某个地方错过了导入或库?androidx.compose.ui.Modifier在班级本身。我知道我在其他地方看到过这是一个常见的修改器问题。
它是一个单独的,您必须在依赖项中指定。
implementation 'com.google.accompanist:accompanist-pager-indicators:0.27.1'
Run Code Online (Sandbox Code Playgroud)
从 Jetpack Compose 1.4.0-alpha03 开始,有一个本机HorizontalPager. androidx.compose.foundation.pager.HorizontalPager您不必使用 pagerTabIndicatorOffset。
样本
@Composable
private fun HomeContent() {
val pagerState: PagerState = rememberPagerState(initialPage = 0)
val coroutineScope = rememberCoroutineScope()
TabRow(
selectedTabIndex = pagerState.currentPage
) {
// Add tabs for all of our pages
tabList.forEachIndexed { index, title ->
Tab(
text = { Text(title) },
selected = pagerState.currentPage == index,
onClick = {
coroutineScope.launch {
pagerState.animateScrollToPage(index)
}
},
)
}
}
HorizontalPager(
state = pagerState,
pageCount = tabList.size,
beyondBoundsPageCount = 3
) { page: Int ->
when (page) {
}
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
2379 次 |
| 最近记录: |