Kotlin中基于ViewBinding实现RecyclerView条目跳转Activity的点击事件
为RecyclerView条目添加点击跳转(Kotlin+ViewBinding实现)
这里提供两种可行的实现方式,推荐使用接口回调的方式解耦UI展示和跳转逻辑。
方法一:接口回调(推荐)
这种方式将点击事件的处理逻辑交给Activity,让Adapter只负责UI渲染,符合单一职责原则。
步骤1:修改CardAdapter,添加点击接口
package com.truuce.anotherrvtest import android.view.LayoutInflater import android.view.View import android.view.ViewGroup import androidx.recyclerview.widget.RecyclerView import com.truuce.anotherrvtest.databinding.HeroCardBinding // 定义点击回调接口 interface OnHeroClickListener { fun onHeroClick(hero: Hero) } class CardAdapter( val heroList: List<Hero>, private val listener: OnHeroClickListener // 添加接口参数 ) : RecyclerView.Adapter<CardAdapter.MainViewHolder>() { inner class MainViewHolder(val heroBinding: HeroCardBinding) : RecyclerView.ViewHolder(heroBinding.root), View.OnClickListener { init { // 给条目根视图设置点击监听 heroBinding.root.setOnClickListener(this) } fun bindHero(hero: Hero){ heroBinding.heroNameTV.text = hero.heroName heroBinding.heroIV.setImageResource(hero.image) } override fun onClick(v: View?) { // 获取当前点击的条目位置,排除无效位置 val position = adapterPosition if (position != RecyclerView.NO_POSITION) { // 触发回调,传递当前Hero对象 listener.onHeroClick(heroList[position]) } } } override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainViewHolder { return MainViewHolder(HeroCardBinding.inflate(LayoutInflater.from(parent.context), parent, false)) } override fun onBindViewHolder(holder: MainViewHolder, position: Int) { val hero = heroList[position] holder.bindHero(hero) } override fun getItemCount() = heroList.size }
步骤2:在HeroActivity中实现跳转逻辑
package com.truuce.anotherrvtest import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.recyclerview.widget.GridLayoutManager import android.content.Intent import com.truuce.anotherrvtest.databinding.ActivityHeroBinding class HeroActivity : AppCompatActivity() { var binding: ActivityHeroBinding? = null override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) binding = ActivityHeroBinding.inflate(layoutInflater) setContentView(binding?.root) // 创建Adapter时传入点击回调实现 val adapter = CardAdapter(HeroList.heroList, object : CardAdapter.OnHeroClickListener { override fun onHeroClick(hero: Hero) { // 启动目标Activity,替换成你实际的Activity类名 val intent = Intent(this@HeroActivity, TargetActivity::class.java) // 传递Hero数据(可选) intent.putExtra("HERO_NAME", hero.heroName) intent.putExtra("HERO_IMAGE", hero.image) startActivity(intent) } }) binding?.heroRV?.adapter = adapter binding?.heroRV?.layoutManager = GridLayoutManager(applicationContext, 3) } override fun onDestroy() { super.onDestroy() binding = null } }
方法二:直接在ViewHolder中处理跳转
如果不需要解耦,也可以直接在ViewHolder内部处理点击跳转:
修改CardAdapter的MainViewHolder
inner class MainViewHolder(val heroBinding: HeroCardBinding) : RecyclerView.ViewHolder(heroBinding.root), View.OnClickListener { init { heroBinding.root.setOnClickListener(this) } fun bindHero(hero: Hero){ heroBinding.heroNameTV.text = hero.heroName heroBinding.heroIV.setImageResource(hero.image) } override fun onClick(v: View?) { val position = adapterPosition if (position != RecyclerView.NO_POSITION) { val hero = heroList[position] val context = v?.context ?: return // 启动目标Activity,替换成你实际的类名 val intent = Intent(context, TargetActivity::class.java) intent.putExtra("HERO_NAME", hero.heroName) intent.putExtra("HERO_IMAGE", hero.image) context.startActivity(intent) } } }
注意事项
- 替换代码中的
TargetActivity为你实际要跳转的Activity类名,并且确保该Activity已在AndroidManifest.xml中注册。 - 如果需要传递更多Hero数据,可以根据Hero类的属性添加对应的
intent.putExtra()参数。
内容的提问来源于stack exchange,提问作者Truuce
相关产品推荐
相关产品推荐

