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

如何在Fragment的RecyclerView中点击条目跳转至不同Activity

解决方案

步骤1:为Adapter添加点击回调接口

在Adapter中定义回调接口,将列表项的点击事件暴露给HomeFragment处理,是RecyclerView点击事件的标准实现方案。
修改后的Adapter.kt代码如下:

class Adapter (val listItem:ArrayList<Locations>) : RecyclerView.Adapter<Adapter.RecycleViewHolder>(){
    // 新增点击回调接口
    interface OnItemClickListener {
        fun onItemClick(location: Locations, position: Int)
    }
    // 外部可赋值的回调实例
    var onItemClickListener: OnItemClickListener? = null

    inner class RecycleViewHolder(itemView: View):RecyclerView.ViewHolder(itemView){
        val itemImage: ShapeableImageView = itemView.findViewById(R.id.item_image)
        val heading: TextView = itemView.findViewById(R.id.item_title)
        val detail: TextView = itemView.findViewById(R.id.item_detail)
        val category: TextView = itemView.findViewById(R.id.item_categories)
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecycleViewHolder {
        val view:View = LayoutInflater.from(parent.context).inflate(R.layout.list_item, parent, false)
        return RecycleViewHolder(view)
    }

    override fun getItemCount(): Int {
        return listItem.size
    }

    override fun onBindViewHolder(holder: RecycleViewHolder, position: Int) {
        val item = listItem[position]
        holder.itemImage.setImageResource(item.itemImage)
        holder.heading.text = item.headings
        holder.detail.text = item.detail
        holder.category.text = item.category
        // 新增列表项点击监听
        holder.itemView.setOnClickListener {
            onItemClickListener?.onItemClick(item, position)
        }
    }
}

步骤2:修改HomeFragment逻辑,处理跳转

首先修复原有代码重复创建Adapter的冗余问题,再实现点击回调,根据点击项的属性判断跳转目标。
修改后的HomeFragment.kt代码如下:

class HomeFragment : Fragment() {
    private var _binding: FragmentHomeBinding? = null
    private val binding get() = _binding!!

    lateinit var recycle1:RecyclerView
    private val list = ArrayList<Locations>()
    private val adapter:Adapter = Adapter(list)

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {

        var v =inflater.inflate(R.layout.fragment_home, container, false)

        recycle1 = v.findViewById(R.id.rv_item)
        recycle1.layoutManager = LinearLayoutManager(activity)

        list.clear()
        testData()

        // 删掉冗余的adapterr实例,直接用全局初始化的adapter
        recycle1.adapter = adapter
        recycle1.setHasFixedSize(true)

        // 新增点击事件处理
        adapter.onItemClickListener = object : Adapter.OnItemClickListener {
            override fun onItemClick(location: Locations, position: Int) {
                // 根据分类跳转不同Activity,可根据需求修改判断逻辑
                val intent = when(location.category) {
                    "Cafe" -> Intent(requireContext(), CafeDetailActivity::class.java)
                    "Library" -> Intent(requireContext(), LibraryDetailActivity::class.java)
                    else -> Intent(requireContext(), DefaultDetailActivity::class.java)
                }
                // 可选:将地点信息传到目标Activity
                intent.putExtra("location_name", location.headings)
                intent.putExtra("location_address", location.detail)
                intent.putExtra("location_category", location.category)
                startActivity(intent)
            }
        }
        return v
    }

    private fun testData(){
        list.add(Locations(R.drawable.book_cafe,"The Book Cafe","20 Martin Rd","Cafe"))
        list.add(Locations(R.drawable.citysprouts,"City Sprouts","102 Henderson Road","Cafe"))
        list.add(Locations(R.drawable.esplanade,"Library@esplande","8 Raffles Ave","Library"))
        list.add(Locations(R.drawable.hf,"Library@Harbourfront","1 HarbourFront Walk","Library"))
        list.add(Locations(R.drawable.mangawork,"MangaWork","291 Serangoon Rd","Cafe"))
        list.add(Locations(R.drawable.orchard,"Library@orchard","277 Orchard Road","Library"))
        list.add(Locations(R.drawable.rabbitandfox,"Rabbit&Fox","160 Orchard Rd","Cafe"))
        list.add(Locations(R.drawable.sixlettercoffee,"T6 Letter Coffee","259 Tanjong Katong Rd","Cafe"))
    }
}

补充说明

  • 如果需要每个单独的地点都跳转完全独立的Activity,可以把when的判断条件改为location.headings,匹配对应的地点名称跳转对应页面即可
  • 所有需要跳转的Activity必须提前在AndroidManifest.xml中注册
  • 目标Activity可以通过intent.getStringExtra("location_name")等方法获取传递过来的参数

内容的提问来源于stack exchange,提问作者Wee Kiat Lim

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.10.05 11:42:03