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

LazyVerticalGrid类型不匹配求助:it被识别为Integer而非Topic

问题解决:LazyVerticalGrid中参数类型错误(it被识别为Integer而非Topic)

问题原因

核心问题是导入了错误的items函数,导致LazyVerticalGrid调用了接收整数数量的重载方法,而非适配List<Topic>的版本,因此Lambda参数被识别为代表索引的Integer类型。

解决方案

  1. 导入正确的items扩展函数
    确保代码中导入的是LazyVerticalGrid专属的items函数,而非其他组件(如LazyColumn)的版本:

    import androidx.compose.foundation.lazy.grid.items
    
  2. 优化代码可读性(可选)
    为Lambda参数指定明确名称,替代默认的it,避免类型歧义:

    @Composable
    fun TopicGrid(modifier: Modifier = Modifier) {
        LazyVerticalGrid(
            columns = GridCells.Fixed(3)
        ) {
            items(topics) { topic ->
                TopicCard(topic)
            }
        }
    }
    

修正后的关键代码片段

// 必须导入该包下的items函数
import androidx.compose.foundation.lazy.grid.items

@Composable
fun TopicGrid(modifier: Modifier = Modifier) {
    LazyVerticalGrid(
        columns = GridCells.Fixed(3)
    ) {
        items(topics) { topic ->
            TopicCard(topic)
        }
    }
}

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.14 01:23:18