Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/kotlin/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/variables/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Kotlin 有没有办法制作带有自动滚动的双向水平寻呼机?_Kotlin_Horizontal Scrolling_Android Jetpack Compose - Fatal编程技术网

Kotlin 有没有办法制作带有自动滚动的双向水平寻呼机?

Kotlin 有没有办法制作带有自动滚动的双向水平寻呼机?,kotlin,horizontal-scrolling,android-jetpack-compose,Kotlin,Horizontal Scrolling,Android Jetpack Compose,来自的HorizontalPager用于创建简单的ViewPager;有没有一种方法可以在两端无限滑动 @ExperimentalPagerApi @Composable fun AutoScrollPagerHorizontal(d: List<Stem>?) { var data: MutableList<Stem> = d?.toMutableList() ?: mutableListOf() if (data.isNullOrEmpty()) r

来自的HorizontalPager用于创建简单的ViewPager;有没有一种方法可以在两端无限滑动

@ExperimentalPagerApi
@Composable
fun AutoScrollPagerHorizontal(d: List<Stem>?) {
    var data: MutableList<Stem> = d?.toMutableList() ?: mutableListOf()

    if (data.isNullOrEmpty()) return
    val pageState = rememberPagerState(pageCount = data.size)
    HorizontalPager(
        state = pageState
    ) {
        Card(
            Modifier
                .height(240.dp)
                .padding(start = 16.dp, end = 16.dp, top = 8.dp, bottom = 8.dp)
        ) {
            Image(
                painter = rememberGlidePainter(
                    request = data[it].icon,
                ),
                contentDescription = data[it].title,
                contentScale = ContentScale.FillBounds
            )
        }
    }
}
@ExperimentalPagerApi
@组合的
趣味AutoScrollPagerOrizontal(d:列表?){
变量数据:MutableList=d?.toMutableList()?:mutableListOf()
if(data.isNullOrEmpty())返回
val pageState=rememberPagerState(pageCount=data.size)
水平寻呼机(
状态=页面状态
) {
卡片(
修饰语
.高度(240.dp)
.填充(开始=16.dp,结束=16.dp,顶部=8.dp,底部=8.dp)
) {
形象(
画家=回忆画家(
请求=数据[it]。图标,
),
contentDescription=数据[it]。标题,
contentScale=contentScale.FillBounds
)
}
}
}

此代码正确生成viewpager,但在到达数据的最后一个索引后不会滚动到第0个索引。

您可以在可组合文件中使用此代码段自动滚动寻呼机:

LaunchedEffect(key1 = pagerState.currentPage) {
    launch {
        delay(3000)
        with(pagerState) {
            val target = if (currentPage < pageCount - 1) currentPage + 1 else 0

            animateScrollToPage(
                page = target,
                animationSpec = tween(
                    durationMillis = 500,
                    easing = FastOutSlowInEasing
                )
            )
        }
    }
}
launchedefect(key1=pagerState.currentPage){
发射{
延迟(3000)
带(页状态){
val target=if(currentPage
指针输入帮助构建双向寻呼机;下面是对我有用的片段

Modifier.pointerInput(Unit) {
                    detectHorizontalDragGestures { change, dragAmount ->
                        change.consumeAllChanges()
                        when {
                            dragAmount < 0 -> {
                                coroutineScope.launch { /* right */
                                    if (pageState.currentPage == data.lastIndex) {
                                        pageState.animateScrollToPage(0)
                                    } else {
                                        pageState.animateScrollToPage(pageState.currentPage + 1)
                                    }
                                }
                            }
                            dragAmount > 0 -> { /* left */
                                coroutineScope.launch {
                                    if (pageState.currentPage == 0) {
                                        pageState.animateScrollToPage(data.lastIndex)
                                    } else {
                                        pageState.animateScrollToPage(pageState.currentPage - 1)
                                    }
                                }
                            }
                        }
                    }
                }
Modifier.pointerInput(单位){
DetectThorzontalDragestures{更改,dragAmount->
change.consumerallchanges()
什么时候{
德拉格蒙特<0->{
coroutineScope.launch{/*右*/
if(pageState.currentPage==data.lastIndex){
pageState.AnimateScrolltPage(0)
}否则{
pageState.AnimateScrolltPage(pageState.currentPage+1)
}
}
}
dragAmount>0->{/*左*/
协同观测发射{
如果(pageState.currentPage==0){
pageState.AnimateScrolltPage(data.lastIndex)
}否则{
pageState.AnimateScrolltPage(pageState.currentPage-1)
}
}
}
}
}
}

自动滚动部分还可以,我更关心HorizontalPager的双向风格;你知道怎么做吗?