繁体   English   中英

使用视图绑定将 onclicklistener 添加到回收器视图,kotlin

[英]adding onclicklistener to recycler view with viewbinding, kotlin

尝试将 onclicklistener 添加到我的回收站视图中的项目,这将使用意图打开另一个活动。 我试过查找示例,但我只能找到使用 Java 或 Kotlin 示例但未使用视图绑定的示例。

package com.truuce.anotherrvtest

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import androidx.recyclerview.widget.GridLayoutManager

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)

        val adapter = CardAdapter(HeroList.heroList)
        binding?.heroRV?.adapter = adapter
        binding?.heroRV?.layoutManager = GridLayoutManager(applicationContext, 3)

    }

    override fun onDestroy() {
        super.onDestroy()
        binding = null
    }
}
package com.truuce.anotherrvtest

import android.content.Context
import android.content.Intent
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.RecyclerView
import com.truuce.anotherrvtest.databinding.HeroCardBinding

class CardAdapter(val heroList: List<Hero>) : RecyclerView.Adapter<CardAdapter.MainViewHolder>() {

    inner class MainViewHolder(val heroBinding: HeroCardBinding) :
        RecyclerView.ViewHolder(heroBinding.root) { 
        fun bindHero(hero: Hero){
            heroBinding.heroNameTV.text = hero.heroName
            heroBinding.heroIV.setImageResource(hero.image)
        }

    }


    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
}

尝试将 View.OnClickListener 添加到 MainViewHolder,然后实现一个成员。 OnClick(p0: View){},但不知道如何让它工作。

您应该在适配器中为点击监听器添加一个功能属性。

Activity 可以设置项目点击侦听器行为。

class CardAdapter(
    val heroList: List<Hero>,
    val itemClickListener: (Hero)->Unit
) : RecyclerView.Adapter<CardAdapter.MainViewHolder>() {

    inner class MainViewHolder(val heroBinding: HeroCardBinding) :
        RecyclerView.ViewHolder(heroBinding.root) { 
        fun bindHero(hero: Hero) = with(heroBinding) {
            heroNameTV.text = hero.heroName
            heroIV.setImageResource(hero.image)
            root.setOnClickListener { itemClickListener(hero) }
        }

    }

    //...
}
// In Activity:

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityHeroBinding.inflate(layoutInflater)
        setContentView(binding?.root)

        val adapter = CardAdapter(HeroList.heroList) { hero ->
            // do something with hero item when it's clicked
        }
        binding?.heroRV?.adapter = adapter
        binding?.heroRV?.layoutManager = GridLayoutManager(applicationContext, 3)

    }
①如果本文未解决您的问题,请点击查看与本文相关的问题
②如果本文未解决您的问题,请向程序员专用AI小助手提问
暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM