简体   繁体   English

如何避免recyclerview中每个小部件的requestModelBuild

[英]How to avoid requestModelBuild for every widgets in a recyclerview

I have a recycler view with fixed number widgets vertically in a specific order.我有一个按特定顺序垂直固定数量小部件的回收站视图。 Some of the widgets also contain tabular data hence I've considered using nested recycler view also within it.一些小部件还包含表格数据,因此我考虑在其中也使用嵌套的回收器视图。

Every widget makes http call asynchronously from the ViewModel and binds the data to the epoxyController as I mentioned below.每个小部件都会使 http 从 ViewModel 异步调用,并将数据绑定到环氧树脂控制器,如下所述。

As requestModelBuild() being called for every widget as they receive the data through the public setters for example priceViewData, packageData and etc from where requestModelBuild() is called.由于每个小部件都会调用 requestModelBuild(),因为它们通过公共设置器(例如 priceViewData、packageData 等)从调用 requestModelBuild() 的位置接收数据。 So in this instance every widget bind happens regardless of every time when data is received for any of the widgets.因此,在这种情况下,无论何时收到任何小部件的数据,都会发生每个小部件绑定。 This seems to be expensive also, there some analytics gets fired as we needed for every bind.这似乎也很昂贵,因为我们每次绑定都需要一些分析。

So, here the analytics call for the widget is multiplied.因此,这里对小部件的分析调用成倍增加。 Please suggest if this can be handled through the epoxy without handling manually.请建议是否可以通过环氧树脂处理而无需手动处理。

class ProductDetailsEpoxyController(val view: View?,
                              private val name: String?,
                              private val context: Context?) : 
 AsyncEpoxyController() {

private val args = bundleOf("name" to name)

var priceViewData: IndicativePriceViewData? = emptyPriceViewData()
    set(value) {
        field = value
        requestModelBuild()
    }

var packageData: PackageViewData? = emptyPackageWidgetViewData()
    set(value) {
        field = value
        requestModelBuild()
    }

   ...
   ...



override fun buildModels() {
    buildPriceViewData()
    buildPackageViewData()
    ....
}

private fun buildPriceViewData(){
    priceViewData?.let {
        id("price")
        priceViewDataModel(it)
    }
}

private fun buildPackageViewData(){
    packageViewData?.let {
         id("package")
          packageViewDataModel(it)
    }
}

  ...
  ...

 }

From Epoxy's Wiki :来自环氧树脂的维基

Adapter and diffing details Once models are built, Epoxy sets the new models on the backing adapter and runs a diffing algorithm to compute changes against the previous model list.适配器和差异详细信息 构建模型后,Epoxy 会在支持适配器上设置新模型并运行差异算法来计算针对先前 model 列表的更改。 Any item changes are notified to the RecyclerView so that views can be removed, inserted, moved, or updated as necessary.任何项目更改都会通知 RecyclerView,以便可以根据需要删除、插入、移动或更新视图。

So basicallly, this ensures not all models will be updated.所以基本上,这确保了并非所有模型都会更新。

The issue that you're facing is possibly related to:您面临的问题可能与以下方面有关:

  1. Using DataBinding使用数据绑定
  2. Your classes are not implemented equals and hashCode the way you want.你的类没有按照你想要的方式实现equalshashCode

The problem with using Objects in DataBinding is that, every time the object is updated, all fields that depend on the object are also updated, even if not all changed.在 DataBinding 中使用对象的问题在于,每次更新 object 时,所有依赖于 object 的字段也会更新,即使不是全部更改。

If your classes are normal classes and not data classes or you expect a different behavior when executing priceData1 == priceData2 (for example, only comparing the data's id), you should override this methods so Epoxy detect changes correctly.如果您的类是普通类而不是数据类,或者您在执行priceData1 == priceData2时期望不同的行为(例如,仅比较数据的 id),您应该重写此方法,以便 Epoxy 正确检测更改。 Also, you can use DoNotHash option for EpoxyAttribute so the class is not added to the model's hashCode function.此外,您可以为 EpoxyAttribute 使用DoNotHash选项,因此 class 不会添加到模型的 hashCode function 中。 More info更多信息

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

相关问题 如何解决在xml布局中的RecyclerView上消失的小部件? - How to solve widgets disappearing on RecyclerView in xml layouts? 每次在 RecyclerView 中显示项目时,如何避免从在线存储 (Firebase) 下载数据? - How to avoid downloading data from online storage (Firebase) every time an item shows in a RecyclerView? 如何避免在RecyclerView中使用反射 - How to avoid using Reflection with RecyclerView 如何避免 imageview 在 recyclerview 中闪烁? - How to avoid imageview blink in recyclerview? 如何避免在主屏幕中创建多个小部件? - How to avoid to creating multiple widgets in homescreen? RecyclerView如何在每个项目上设置OnClickListener? - RecyclerView how to setOnClickListener on every item? 如何避免 RecyclerView ViewHolder 中的内存泄漏? - How to avoid memory leaks in RecyclerView ViewHolder? 如何避免在RecyclerView OnClickListener上进行双重选择? - How to avoid double selection on a RecyclerView OnClickListener? Mediaplayer如何避免在RecyclerView中播放多个音频 - Mediaplayer how to avoid playing multiple audios in a RecyclerView 如何避免RecyclerView中的数据重复 - Android? - How to avoid data repetition in RecyclerView - Android?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM