You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让分组后的LazyColumn默认定位到指定状态分类?

问题描述

我通过API获取的数据在LazyColumn中实现了两级分组(一级按status分组,二级按round分组),每个状态分类下包含多个回合分类,每个回合分类下有若干元素。我希望LazyColumn默认显示指定的状态分类,但由于API数据会动态变化,无法使用animateScrollToItem方法。相关代码如下:

LazyColumn {
        byStatusList.values.forEachIndexed { index, fixturesList->
            item {
                Spacer(modifier = Modifier
                    .fillMaxWidth()
                    .height(10.dp)
                )
            }
            item(key = "header_$index") {
                Row(
                    modifier = Modifier
                        .fillMaxWidth()
                        .padding(8.dp)
                ) {
                    Text(
                        text = fixturesList[index]!!
                              .fixture!!.status!!.jsonMemberLong!!
                    )
                }
            }
            val byRoundList = fixturesList.groupBy { it!!.league!!.round }
            byRoundList.forEach { (round, list)->
                item {
                    Spacer(modifier = Modifier
                        .fillMaxWidth()
                        .height(10.dp)
                    )
                }
                item {
                    RoundMatchesHeader(round!!)
                }
                items(list.size) {
                    RoundMatchesItems(fixture = list[it]!!)
                }
            }
        }
    }
解决方案

核心思路是调整数据集的加载顺序,将目标状态分类放到数据集的最前端,让LazyColumn默认从该分类开始显示,完全规避滚动定位API的局限性,适配动态数据场景。

具体修改步骤

  1. 对分组后的状态列表进行排序,将目标状态对应的列表移至首位:
// 定义需要默认显示的目标状态(根据实际业务值修改)
val targetStatus = "Live"

// 重新排序状态列表:目标状态排第一,其余保持原顺序
val sortedStatusLists = byStatusList.values.sortedBy { fixturesList ->
    val currentStatus = fixturesList.firstOrNull()?.fixture?.status?.jsonMemberLong ?: ""
    // 目标状态返回0,其余返回1,确保排序后目标状态在最前
    if (currentStatus == targetStatus) 0 else 1
}
  1. 将LazyColumn中遍历的数据源替换为排序后的列表,原有分组逻辑保持不变(同时优化空值处理):
LazyColumn {
    sortedStatusLists.forEachIndexed { index, fixturesList ->
        item {
            Spacer(modifier = Modifier
                .fillMaxWidth()
                .height(10.dp)
            )
        }
        item(key = "header_$index") {
            Row(
                modifier = Modifier
                    .fillMaxWidth()
                    .padding(8.dp)
            ) {
                Text(
                    text = fixturesList.firstOrNull()?.fixture?.status?.jsonMemberLong ?: ""
                )
            }
        }
        val byRoundList = fixturesList.groupBy { it?.league?.round }
        byRoundList.forEach { (round, list) ->
            item {
                Spacer(modifier = Modifier
                    .fillMaxWidth()
                    .height(10.dp)
                )
            }
            item {
                round?.let { RoundMatchesHeader(it) }
            }
            items(list.orEmpty()) { fixture ->
                fixture?.let { RoundMatchesItems(fixture = it) }
            }
        }
    }
}

额外优化说明

  • 移除了原代码中的!!非空断言,改用firstOrNull()、?.let()和list.orEmpty()处理空值,避免API数据异常时出现空指针崩溃
  • 排序逻辑完全基于数据本身,不受列表长度变化影响,完美适配API动态更新的场景

内容的提问来源于stack exchange,提问作者Arthur Thuma

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.27 10:46:21