jetpack Compose版底纹词组件

随着谷歌推广Jetpack Compsoe框架,我们项目也积极拥抱变化在新业务页面中使用Compsoe技术开发。开发过程成遇到底纹词组件网上没找到方案,于是就手撸了一个compose版本的底纹词组件,废话不多说代码如下:

@Composable
fun TextBanner(
    modifier: Modifier = Modifier,
    list: List<String> = arrayListOf(),
    delayMillis: Long = 3000, // 等待3秒
    onItemChange: (String) -> Unit = {}
) {
    val scope = rememberCoroutineScope()
    var currentIndex by remember { mutableIntStateOf(0) }

    LaunchedEffect(key1 = list) {
        scope.launch {
            while (list.isNotEmpty()) {
                delay(delayMillis)
                currentIndex = (currentIndex + 1) % list.size // 更新索引,实现循环滚动
                onItemChange(list[currentIndex])
            }
        }
    }
    if(list.isNotEmpty()){
        AnimatedContent(
            targetState = currentIndex,
            modifier = modifier,
            transitionSpec = {
                (slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Up) togetherWith
                        slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Up))
            },
            contentAlignment = Alignment.Center
        ) { index ->
            Text(
                text = list[index],
                modifier = Modifier.wrapContentHeight(),
                color = Color(0xFFA4A4A4),
                fontSize = 14.sp
            )
        }
    }
}

希望对你有用!

在上个版本基础上添加了生命周期关联,主要是切出页面后要暂停循环,切回页面后继续文本循环,代码如下:

@Composable
fun TextBanner(
    modifier: Modifier = Modifier,
    list: List<String> = arrayListOf(),
    delayMillis: Long = 3000, // 等待3秒
    onItemChange: (String) -> Unit = {}
) {
    val scope = rememberCoroutineScope()
    var currentIndex by remember { mutableIntStateOf(0) }
    var isLooping by remember { mutableStateOf(false) }

    // 监听生命周期
    val lifecycle = LocalLifecycleOwner.current.lifecycle
    DisposableEffect(lifecycle) {
        val lifecycleObserver = LifecycleEventObserver { _, event ->
            when (event) {
                Lifecycle.Event.ON_RESUME -> {
                    isLooping = true
                }
                Lifecycle.Event.ON_STOP -> {
                    isLooping = false
                }
                else -> {}
            }
        }
        lifecycle.addObserver(lifecycleObserver)
        onDispose {
            lifecycle.removeObserver(lifecycleObserver)
        }
    }

    LaunchedEffect(key1 = isLooping) {
        scope.launch {
            while (isLooping && list.isNotEmpty()) {
                delay(delayMillis)
                currentIndex = (currentIndex + 1) % list.size // 更新索引,实现循环滚动
                onItemChange(list[currentIndex])
            }
        }
    }

    if(list.isNotEmpty()){
        AnimatedContent(
            targetState = currentIndex,
            modifier = modifier,
            transitionSpec = {
                (slideIntoContainer(AnimatedContentTransitionScope.SlideDirection.Up) togetherWith
                        slideOutOfContainer(AnimatedContentTransitionScope.SlideDirection.Up))
            },
            contentAlignment = Alignment.Center, label = ""
        ) { index ->
            Text(
                text = list[index],
                modifier = Modifier.wrapContentHeight(),
                color = Color(0xFFA4A4A4),
                fontSize = 14.sp
            )
        }
    }
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值