需要解释为什么RecyclerView.Adapter.notifyItemChanged(int position,Object有效负载)和我绑定的有效负载参数是对象列表 [英] Need an explanation why RecyclerView.Adapter.notifyItemChanged(int position, Object payload) and I bind payload parameter is List of objects

查看:318
本文介绍了需要解释为什么RecyclerView.Adapter.notifyItemChanged(int position,Object有效负载)和我绑定的有效负载参数是对象列表的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

RecyclerView.Adapter.notifyItemChanged(int位置,对象有效载荷),其中有效载荷是一个任意对象,将传递给RecyclerView.Adapter.onBindViewHolder(VH持有人,int位置,列出有效载荷)

RecyclerView.Adapter.notifyItemChanged(int position, Object payload), where payload is an arbitrary object that will be passed to RecyclerView.Adapter.onBindViewHolder(VH holder, int position, List payloads)

那么,如果我不能传递的项目数超过1个,为什么还要通过bind方法传递的有效载荷列表呢?

So why list of payloads passed in the bind method if I can't pass than 1 object on item changed?

推荐答案

您还可以将有效负载与DiffUtils或notifyItemChanged(int position, @Nullable Object payload)

You can use payloads with also DiffUtils or with notifyItemChanged(int position, @Nullable Object payload)

作为列表的Object/Any类的有效负载点,根据您使用的方案具有不同种类的对象.

The point of payload of being an Object/Any class of list to have different kind of objects depending on scenario you have.

例如,假设您的回收站视图项目包含图像,标题(字符串)和价格(数字)或其他任何值(视情况而定).

For instance let's say your recycler view items contain image, title as string and price as number, or any other values depending on situation.

如果在没有有效负载的情况下调用onBindViewHolder时数据中的某些字段发生变化,并且图像较大,则闪烁的图像会打扰用户.

When some fields in your data change when onBindViewHolder is called without payload, and you have a big image you have a flickering which bothers users.

您可以根据更改的数据来检查并发送有效负载. 如果仅标题已更改,则在此列表中放置一个字符串;如果数字已更改,则可以将Int置于list [0].如果两者都更改,则可以将标题添加到列表[0],将价格添加到列表[1],然后检查这种情况. String和Int是简单变量,不需要发送Int而不是String,但是出于参数的考虑,有效载荷中可以包含不同的项目.

Instead you check and send payload depending on which data changed. If only title has changed you put a String inside this list, if number has changed you can put Int to list[0]. If both change you can put title to list[0] and price to list[1] and check for that situation. String and Int are simple variables and no need to send Int instead of String but these are for the sake of the argument, you can have different items in payloads.

override fun onBindViewHolder(
    holder: CustomViewHolder<CurrencyRateEntity>,
    position: Int,
    payloads: MutableList<Any>
) {


    if (payloads.isNullOrEmpty()) {

        // Set item binding as whole since no payload
        binding.setVariable(BR.item, item)
        binding.executePendingBindings()

    } else {

        when (val payload = payloads[0]) {
            is String -> {
                title.setText = payloads[0]
            }
            is Int -> {
                // do something with int
                price.text =  "${payload / 1000}"
            }
        }
    }

    super.onBindViewHolder(holder, position, payloads)
}

这篇关于需要解释为什么RecyclerView.Adapter.notifyItemChanged(int position,Object有效负载)和我绑定的有效负载参数是对象列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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