compose LazyListState

rememberLazyListState 是 Jetpack Compose 中用于控制 LazyColumn 或 LazyRow 滚动行为的一个状态管理工具

val horizonListState = rememberLazyListState()

 LazyRow(
                state = listState,
                modifier = Modifier
                    .fillMaxWidth()
                    .wrapContentHeight(),
                reverseLayout = true
            ) 
。。。。。。

通过 LazyListState我们可以:

1.控制列表滚动

例如下面,当用户点击选项触发 selectedIndex 变化之后,可以通过如下代码,将列表滚动到 当前选中的item 

    LaunchedEffect(selectedIndex) {
        LogUtils.d("LaunchedEffect 当前选中项 selectedIndex=$selectedIndex,")
        val center = horizonListState.layoutInfo.viewportEndOffset / 2
        val selectedItem = horizonListState.layoutInfo.visibleItemsInfo.getOrNull(selectedIndex + 1)
        selectedItem?.let {
            horizonListState.scrollToItem(selectedIndex + 1, -center + it.size / 2)
        }
    }

2 监听滚动位置,触发自动加载

   // 横向列表统计数据无限加载检测
    LaunchedEffect(horizonListState, currentStatistics) {
        snapshotFlow { horizonListState.layoutInfo }.collect { layoutInfo ->
            //自动加载
            if (layoutInfo.visibleItemsInfo.isNotEmpty() && !currentStatistics.isNullOrEmpty() && layoutInfo.visibleItemsInfo.last().index >= currentStatistics!!.size - 3) {
                loadMoreStatistic(viewModel, viewType)
            }
        }
    }

### Jetpack Compose 中 Scrollbar 的实现 Jetpack Compose 提供了一种现代化的方式来构建 Android UI,其中 `LazyColumn` 和 `LazyRow` 是常用的可滚动列表组件。为了增强用户体验,可以通过第三方库或自定义方式来实现滚动条功能。 #### 使用 LazyColumnScrollbar 实现滚动条 一个流行的解决方案是使用名为 **LazyColumnScrollbar** 的开源项目[^1]。该项目提供了一个简单的 API 来为 `LazyColumn` 添加滚动条支持。以下是其实现的核心逻辑: ```kotlin import androidx.compose.foundation.lazy.LazyListState import androidx.compose.runtime.Composable import com.example.scrollbarscrollbar.ScrollbarDecoration @Composable fun LazyColumnWithScrollbar( listState: LazyListState, content: @Composable () -> Unit ) { Box(modifier = Modifier.fillMaxSize()) { content() ScrollbarDecoration(listState = listState) } } ``` 在此代码片段中,`ScrollbarDecoration` 负责绘制滚动条的视觉效果,而 `listState` 则跟踪当前滚动位置并更新滚动条的状态[^1]。 --- #### 自定义滚动条实现 除了依赖外部库之外,还可以通过组合 Jetpack Compose 原生的功能来自定义滚动条行为。以下是一个基本示例: ```kotlin import androidx.compose.foundation.gestures.rememberScrollableState import androidx.compose.foundation.verticalScroll import androidx.compose.ui.Modifier import androidx.compose.foundation.layout.Box import androidx.compose.material.Surface import androidx.compose.runtime.getValue import androidx.compose.runtime.mutableStateOf import androidx.compose.runtime.setValue import androidx.compose.ui.graphics.Color import androidx.compose.foundation.layout.width import androidx.compose.foundation.layout.height import androidx.compose.ui.unit.dp @Composable fun CustomScrollbarExample() { var scrollPosition by mutableStateOf(0f) Surface(color = Color.White, modifier = Modifier.fillMaxSize()) { Box { Column( modifier = Modifier .verticalScroll( rememberScrollableState { delta -> scrollPosition += delta -delta } ) ) { repeat(50) { Text("Item $it", modifier = Modifier.padding(vertical = 8.dp)) } } // 绘制滚动条 Box( modifier = Modifier .align(Alignment.CenterEnd) .width(8.dp) .height((scrollPosition / 10).dp) .background(Color.Gray) ) } } } ``` 此代码展示了如何监听垂直滚动事件,并基于当前位置动态调整滚动条的高度和颜色[^2]。 --- #### Accompanist 库的支持 对于更复杂的场景,可以考虑使用 **Accompanist** 这一官方推荐的扩展库。虽然其主要目标是对现有 Material Design 功能进行补充,但在某些情况下也可以辅助实现滚动条功能[^3]。例如,结合 `GlideImage` 或其他组件优化性能的同时,间接提升滚动体验。 --- ### 总结 无论是借助现有的开源项目(如 LazyColumnScrollbar),还是利用 Jetpack Compose 的原生能力自行设计滚动条,都可以满足不同复杂度的需求。具体选择取决于项目的实际需求和技术栈偏好。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值