简体   繁体   English

Jetpack Compose LazyColumn 项目在 stickyHeader 上滚动并且不滚动到最后一个项目

[英]Jetpack Compose LazyColumn items scroll over stickyHeader and does not scroll to last item

I am struggling with the jetpack compose LazyColumn and the stickyHeader functionality.我正在努力使用 jetpack compose LazyColumn 和 stickyHeader 功能。 Basically the static view works well, but once I start scrolling, the items would go over the sticky headers, the scrolling starts a weird behaviour and the last item would never be visible as the scrolling always bounces back.基本上 static 视图运行良好,但一旦我开始滚动,项目将 go 在粘性标题上,滚动开始奇怪的行为并且最后一个项目将永远不可见,因为滚动总是反弹回来。

Here's how it looks like:它是这样的:

在此处输入图像描述

Here's the composable:这是可组合的:

@OptIn(ExperimentalFoundationApi::class)
@Composable
fun CollectionsScreen(
    collectionsLive: LiveData<List<CollectionsView>>,
    onCollectionChanged: (ICalCollection) -> Unit
    /* some more hoisted functions left out for simplicity */
) {

    val list by collectionsLive.observeAsState(emptyList())
    val grouped = list.groupBy { it.accountName ?: it.accountType ?: "Account" }

    LazyColumn(
        modifier = Modifier.padding(8.dp)
    ) {

        item {
            Text(
                stringResource(id = R.string.collections_info),
                textAlign = TextAlign.Center,
                modifier = Modifier.padding(bottom = 16.dp)
            )
        }

        grouped.forEach { (account, collectionsInAccount) ->
            stickyHeader {
                Text(
                    account,
                    style = MaterialTheme.typography.titleLarge,
                    fontWeight = FontWeight.Bold,
                    modifier = Modifier.padding(
                        top = 16.dp,
                        start = 8.dp,
                        end = 16.dp,
                        bottom = 8.dp
                    )
                )
            }

            items(
                items = collectionsInAccount,
                key = { collection -> collection.collectionId }
            ) { collection ->

                CollectionCard(
                    collection = collection,
                    allCollections = list,
                    onCollectionChanged = onCollectionChanged,
                    /* some more hoisted functions left out for simplicity */
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(bottom = 8.dp)
                        .animateItemPlacement()
                        .combinedClickable(
                            //onClick = { onCollectionClicked(collection) }
                         )
                )
            }
        }
    }
}

I am really not sure what is causing this issue as the code itself is pretty straightforward from the example provided in the documentation.我真的不确定是什么导致了这个问题,因为代码本身从文档中提供的示例中非常简单。 Only the CollectionCard itself is a more complex structure.只有 CollectionCard 本身是一个更复杂的结构。 I have also tried removing the header text (the first item) and removed the Modifier.animateItemPlacement() for the card, but with no difference, the problem stays the same... The composable itself is used in a Compose View within a Fragment, but there is no nested scrolling.我也尝试删除 header 文本(第一项)并删除卡的 Modifier.animateItemPlacement() ,但没有区别,问题保持不变......可组合本身用于 Fragment 内的 Compose View , 但没有嵌套滚动。 Do you have any idea what could cause this strange behaviour?你知道什么会导致这种奇怪的行为吗? Or might this be a bug when using cards within the LazyColumn with sticky headers?或者在带有粘性标题的 LazyColumn 中使用卡片时,这可能是一个错误?

UPDATE: It seems like the problem nothing to do with the stickyHeader, but somehow with the LazyColumn.更新:问题似乎与 stickyHeader 无关,但与 LazyColumn 不知何故有关。 If I replace the "stickyHeader" just with "item", the problem still persists... Only when I replace the lazyColumn with a column it would work.如果我用“item”替换“stickyHeader”,问题仍然存在......只有当我用一个列替换 lazyColumn 时它才会起作用。 But I assume that there must be a solution for this problem...但我认为必须有解决这个问题的办法......

Setting the stickyHeader background color will help.设置stickyHeader 背景颜色会有所帮助。

stickyHeader {
                Text(
                    "text",
                    modifier = Modifier.padding(
                        top = 16.dp,
                        start = 8.dp,
                        end = 16.dp,
                        bottom = 8.dp
                    )
                   .background(colorResource(id = R.color.white))
                )
            }

I don't know if you solved it yet, but try to fillMaxWidth and set the background.不知道你解决了没有,还是试试fillMaxWidth和设置背景吧。 This code worked for me.这段代码对我有用。

Text(
          account,
          style = MaterialTheme.typography.titleLarge,
          fontWeight = FontWeight.Bold,
          modifier = Modifier
              .padding(
                       top = 16.dp,
                       start = 8.dp,
                       end = 16.dp,
                       bottom = 8.dp
                      )
              .fillMaxWidth()
              .background(MaterialTheme.colors.background)
    )

Just provide a background for the sticky header.只需为粘性 header 提供背景即可。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM