android 如何使可滚动的LazyRow与分页3与全宽度

moiiocjp  于 2023-09-28  发布在  Android
关注(0)|答案(1)|浏览(145)

如何在屏幕上显示第一个元素而不是最后一个?

val pages = viewModel.getPages().collectAsLazyPagingItems()
LazyRow {
        items(
            items = pages,
            key = { it.id }
        ) { page ->
            Card(
                modifier = Modifier
                    .fillParentMaxWidth()
            ) {

        }
    }

items = pages有三个元素,但卡片在屏幕上显示的是最后一个元素(nextKey),我需要滚动一个元素回来查看被单击的元素。//paging source

override suspend fun load(params: LoadParams<Int>): LoadResult<Int, Page> {
        return 
            println(params.key)
            val page = params.key ?: 1

            val response = repository.getPage(
                account = account,
                maxlen = maxlen
            )
            LoadResult.Page(
                data = response,
                prevKey = page.minus(1),
                nextKey = if (response.isEmpty()) null else page.plus(1),
            )
    }
iyr7buue

iyr7buue1#

看起来我在LazyRow里面需要这个:

val lazyListState = rememberLazyListState()
coroutineScope.launch {
    lazyListState.animateScrollToItem(index = 1)
}

相关问题