在Android Kotlin中,将onclick事件传递给Viewholder的正确方法是什么? [英] In Android Kotlin, what's the right way to pass a onclick event into a viewholder?

查看:173
本文介绍了在Android Kotlin中,将onclick事件传递给Viewholder的正确方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这两种方式是否有区别? 我一直在使用第二种方法,到目前为止,它仍然有效,但是我在阅读教程文章时找到了第一种方法.

Is there any difference in these two ways? I've been using the seond way and it works so far, yet I found the first way upon reading tutorial articles.

第一:

class FlowersAdapter(private val onClick: (Flower) -> Unit) :
ListAdapter<Flower, FlowersAdapter.FlowerViewHolder>(FlowerDiffCallback) {

/* ViewHolder for Flower, takes in the inflated view and the onClick behavior. */
class FlowerViewHolder(itemView: View, val onClick: (Flower) -> Unit) :
    RecyclerView.ViewHolder(itemView) {
    private val flowerTextView: TextView = itemView.findViewById(R.id.flower_text)
    private val flowerImageView: ImageView = itemView.findViewById(R.id.flower_image)
    private var currentFlower: Flower? = null

    init {
        itemView.setOnClickListener {
            currentFlower?.let {
                onClick(it)
            }
        }
    }

    /* Bind flower name and image. */
    fun bind(flower: Flower) {
        currentFlower = flower

        flowerTextView.text = flower.name
        if (flower.image != null) {
            flowerImageView.setImageResource(flower.image)
        } else {
            flowerImageView.setImageResource(R.drawable.rose)
        }
    }
}

}

第一种书写方式

第二:

class FlowerViewHolder(itemView: View) :
    RecyclerView.ViewHolder(itemView) {
    private val flowerTextView: TextView = itemView.findViewById(R.id.flower_text)
    private val flowerImageView: ImageView = itemView.findViewById(R.id.flower_image)
    private var currentFlower: Flower? = null

    /* Bind flower name and image. */
    fun bind(flower: Flower) {
        currentFlower = flower

        flowerTextView.text = flower.name
        if (flower.image != null) {
            flowerImageView.setImageResource(flower.image)
        } else {
            flowerImageView.setImageResource(R.drawable.rose)
        }

        itemView.setOnClickListener {
                onClick(flower)
        }
    }
}

第二种书写方式

感谢您花费时间和精力告诉我这些区别.

Appreicate your time and effort in telling me the differences.

推荐答案

从关注点分离的角度来看,所有clickListeners都应该在ActivityFragmentAdapters中处理只是包裹物品,以您的情况Flower并以RecyclerView可以在屏幕上显示的方式展示它们.

From the perceptive of separation of concern, all the clickListeners are supposed to be handled in the Activity or Fragment and Adapters are meant just to wrap around the items, in your case Flower and present them in a way which can be used by the RecyclerView to display on the screen.

话虽如此,clickListeners的核心逻辑将被移出bind方法,进入活动/片段,这正是firstMethod的全部意义所在.实际上,在第二个方法上使用FirstMethod并没有带来任何性能改进,但我坚持使用FirstOne,因为它具有更多的代码组织功能.

With that being said, the core logic of clickListeners is to be moved out of the bind method into the activity/fragment and that's precisely whats the firstMethod is all about. Matter of fact, I haven't noticed any performance improvement by employing the FirstMethod over the second one yet I insist on using FirstOne because its more of code organizing.

这篇关于在Android Kotlin中,将onclick事件传递给Viewholder的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆