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

holgip5t  于 2023-01-13  发布在  Kotlin
关注(0)|答案(1)|浏览(145)

尝试向回收器视图中的项目添加onclicklistener,它将使用Intent打开另一个Activity。我尝试查找示例,但只能找到使用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){},但不知道如何让它工作。

avwztpqn

avwztpqn1#

应该在适配器中为单击侦听器添加函数属性。
"活动"可以设置项目单击侦听器行为。

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)

    }

相关问题