/**
* Instantly brings the item at [index] to the top of the viewport, offset by [scrollOffset]
* pixels.
*
* Cancels the currently running scroll, if any, and suspends until the cancellation is
* complete.
*
* @param index the data index to snap to
* @param scrollOffset the number of pixels past the start of the item to snap to
*/
@OptIn(ExperimentalFoundationApi::class)
suspend fun snapToItemIndex(
@IntRange(from = 0)
index: Int,
@IntRange(from = 0)
scrollOffset: Int = 0
) = scrollableController.scroll {
scrollPosition.update(
index = DataIndex(index),
scrollOffset = scrollOffset,
// `true` will be replaced with the real value during the forceRemeasure() execution
canScrollForward = true
)
remeasurement.forceRemeasure()
}
@ExperimentalFoundationApi
@Composable
private fun ListView(data: YourClass) {
//this is to remember state, internal API also use the same
val state = rememberLazyListState()
LazyColumn(Modifier.fillMaxSize(), state) {
itemsIndexed(items = data.list, itemContent = { index, dataItem ->
ItemView(dataItem)// your row
})
// header after some data, according to your condition
stickyHeader {
ItemDecoration()// compose fun for sticky header
}
// More items after header
itemsIndexed(items = data.list2, itemContent = { index, dataItem ->
ItemView(dataItem)// your row
})
}
// scroll to top
// I am scrolling to top every time data changes, use accordingly
CoroutineScope(Dispatchers.Main).launch {
state.snapToItemIndex(0, 0)
}
}
}
var index = pagerState.currentPage
LaunchedEffect(true) {
while (true) {
delay(TIME_DELAY_BANNER)
if (index == pagerState.pageCount) {
index = 0
}
pagerState.animateScrollToPage(page = index++)
}
}
5条答案
按热度按时间cu6pst1q1#
LazyListState
**支持通过***
scrollToItem()
**函数,“立即”对齐滚动位置,***
animateScrollToItem()
**使用动画滚动类似于:
weylhg0b2#
在合成1.0.0-alpha07中,没有公共API,但
LazyListState#snapToItemIndex
有一些内部API。也许在即将发布的版本中,我们可以看到更新。
2ledvvac3#
这是我的代码,使粘性标题,列表和滚动
zqdjd7g94#
尝试
2w2cym1i5#
我解决了2个问题
一种方法:我选择了这种最好的方法
两种方式: