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

解决Jetpack Compose中LazyColumn项点击跳转对应歌曲页面的问题

问题解决:点击歌曲项跳转至对应歌曲页面

你的问题核心是跳转时未传递当前点击的歌曲标识,导致HymnActivity无法区分要展示哪首歌,所以所有项都跳转到同一首。以下是修复步骤:

1. 修正SongView的跳转逻辑

在Compose中先获取上下文,通过Intent传递歌曲的编号(number)作为唯一标识:

@Composable
fun SongView(
    song: Song,
) {
    val context = LocalContext.current // 获取Compose上下文

    Box(
        modifier = Modifier
            .fillMaxWidth()
            .padding(2.dp)
            .border(BorderStroke(2.dp, Color.Black))
            .clickable {
                // 创建Intent并传递歌曲编号
                val intent = Intent(context, HymnActivity::class.java)
                intent.putExtra("SONG_NUMBER", song.number)
                context.startActivity(intent)
            },
    ) {
        Text(
            modifier = Modifier
                .fillMaxSize()
                .padding(15.dp),
            text = "${song.text}",
            fontSize = 15.sp,
            fontWeight = FontWeight.SemiBold,
            color = Color.Black
        )

        Text(
            modifier = Modifier
                .fillMaxSize()
                .align(Alignment.TopEnd)
                .padding(15.dp),
            text = "${song.number}",
            fontSize = 20.sp,
            fontWeight = FontWeight.Bold,
            color = Color.Black
        )
    }
}

2. 在HymnActivity中接收并使用传递的数据

在HymnActivity的onCreate方法中获取传递的歌曲编号,根据编号找到对应的歌曲数据并展示:

class HymnActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        // 获取传递的歌曲编号
        val songNumber = intent.getIntExtra("SONG_NUMBER", -1)
        
        if (songNumber != -1) {
            // 根据编号获取对应歌曲数据(复用你项目中的数据获取逻辑)
            val song = listOfSongs().getAllData().find { it.number == songNumber }
            // 用song数据更新UI,比如展示歌词、标题等
        }
    }
}

问题根源说明

你之前的代码仅启动HymnActivity,但未传递任何歌曲标识,导致页面无法区分要加载哪首歌,自然显示同一内容。通过Intent传递唯一的歌曲编号,就能让HymnActivity定位到具体的歌曲数据,解决跳转后内容一致的问题。

内容的提问来源于stack exchange,提问作者Opeoluwa-Muritala

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.11 10:50:47