android compose 左右滑动带指示器,HorizontalPager和HorizontalPagerIndicator使用

android compose 左右滑动带指示器,HorizontalPager和HorizontalPagerIndicator使用

复制代码
/**
 * Author : wn
 * Email : maoning20080809@163.com
 * Date : 2026/1/5 20:58
 * Description : 左右滑动 带指示器, HorizontalPager 和 PagerIndicator
 */
class HorizontalPagerActivity : ComponentActivity(){

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)

        setContent {
            testHorizontalPagerWidthIndicator()
        }
    }

    @OptIn(ExperimentalPagerApi::class)
    @Preview
    @Composable
    private fun testHorizontalPagerWidthIndicator(){
        //页面数量
        val pageCount = 5

        val pagerState = rememberPagerState()

        Column(modifier = Modifier.fillMaxSize(),
            horizontalAlignment = Alignment.CenterHorizontally){

            Spacer(Modifier.height(20.dp))
            Text(text = "左右滑动指示器", fontSize = 30.sp)

            Spacer(Modifier.height(20.dp))

            //水平分页器
            HorizontalPager(
                count = pageCount,
                state = pagerState,
                modifier = Modifier.padding(10.dp)
            ) { page ->
                //每个页面的内容
                Box(modifier = Modifier.width(300.dp).height(400.dp).padding(16.dp)
                    .background(
                        Color(Random.nextInt(256),
                            Random.nextInt(256),
                            Random.nextInt(256),
                            ),
                        shape = RoundedCornerShape(12.dp)
                    ), contentAlignment = Alignment.Center){
                    Text(text = "页面 ${page + 1}", style = MaterialTheme.typography.headlineMedium,
                        color = Color.White)
                }
            }

            Spacer(modifier = Modifier.height(16.dp))

            // 指示器固定在底部
            Box(
                modifier = Modifier.padding(10.dp),
                contentAlignment = Alignment.BottomCenter
            ) {
                HorizontalPagerIndicator(
                    pagerState = pagerState,
                    modifier = Modifier.padding(bottom = 32.dp),
                    activeColor = MaterialTheme.colorScheme.primary,
                    inactiveColor = MaterialTheme.colorScheme.onSurface.copy(alpha = 0.3f)
                )
            }
        }
    }

}
相关推荐
千里马学框架3 天前
一起学 Android 14:ShellTransition 屏幕旋转过程深度剖析
android·智能手机·性能优化·framework·性能·屏幕旋转·rotation
美狐美颜SDK开放平台3 天前
开发直播APP时如何接入视频美颜SDK?开发流程与注意事项
android·人工智能·计算机视觉·音视频·直播美颜sdk
AFinalStone3 天前
Android7 SystemUI源码解析(七)Keyguard锁屏模块深度解析
android·systemui
致远ccc3 天前
Google Play 上架前如何测试 App?多国家 Android 环境测试
android·app测试·googleplay·多国家应用测试
ttyyttemo3 天前
Kotlin 协程中的 Job 结构化并发与取消
android
sun0077003 天前
tbox 4g/5g切换,导致wan ip 改变,导致车机旧网络不可用。需要重启车机才行
android
ai2work3 天前
ch23 综合复刻:从零做一个最小可用版本(capstone)
kotlin
其实防守也摸鱼3 天前
内网穿透与反向代理:原理、工具与实战指南
android·大数据·运维·安全·网络安全·自动化·渗透
ai2work3 天前
ch21 签名、校验与发版
kotlin
AFinalStone3 天前
Android7 SystemUI 源码解析(四)NavigationBar 导航栏与 SystemBars
android·systemui