Android 为什么在 RecyclerView 中使用 executePendingBindings [英] Android Why use executePendingBindings in RecyclerView

查看:37
本文介绍了Android 为什么在 RecyclerView 中使用 executePendingBindings的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近一直在使用数据绑定,并且遇到了 executePendingBindings 方法.该文档几乎没有介绍它,我无法理解它是如何工作的或何时使用它.

I have been using lately data bindings and i came across the executePendingBindings method. The documentation shows little much about it and i can't understand how it works or when to use it.

许多开发人员在 onBindViewHolder 回调中使用 executePendingBindings,但我自己在使用与否时在回收器中没有看到任何区别.

A lot of developers use the executePendingBindings inside the onBindViewHolder callback but i don't see any differences myself in recycler when using it or not.

有人可以解释为什么在回收器中使用它很重要吗??谢谢

Can someone explain why its important to use in recycler?? Thanks

    @Override
    public void onBindViewHolder(final RecyclerView.ViewHolder holder, int position) {

            Customer customer= List.get(position).second;
            ((CustomerViewHolder)holder).binding.setCustomer (customer)

            ((CustomerViewHolder)holder).binding.executePendingBindings();


}

推荐答案

对绑定进行一些更改并不意味着它会对您的 View 产生立即影响.更改绑定中的内容意味着您实际上是在安排在最近的将来应用这些更改.这有很多原因,性能就是其中之一.

Doing some changes on your binding does not mean that it will have an immediate effect on your View. Changing things in binding means that you're really scheduling those changes to be applied in the nearest future. This is for many reasons, performance being one of them.

想象一下,您的 xml 中有一些复杂的表达式.在设置所有绑定变量之前,您不想弄清楚与绑定变量相关的所有内容.那会浪费资源.

Imagine you've got some complex expressions in your xmls. You don't want to figure out everything related to binding variables before you set all of them. That would be wasting of resources.

您可以在生成的绑定 java 类本身中看到更多关于它的信息.我建议你通读一遍.

You can see more about it in the generated binding java class itself. I suggest you read through it.

调用 executePendingBindings 意味着您实际上是在强制框架在调用它的那一刻完成它目前需要在绑定上执行的所有操作.

Calling executePendingBindings means that you're essentially forcing the framework to do everything it needs to do so far on the binding, right at the moment of calling it.

如果您的情况不需要,您不必必须在您的适配器中执行此操作.有些人这样做是为了确保在继续之前在项目上正确设置了所有内容.所以例如没有像 onBind 在上一轮绑定执行之前被再次调用的情况......或类似的......

You don't have to do it in your Adapter if your case does not require that. Some people are doing that to be sure that everything is properly set on an item before moving on. So e.g. there's no case like the onBind being called again before the previous round of bindings was executed... or something similar...

编辑 1:

另外...不要忘记调度执行更改(在最近的将来)是允许您从不同于线程的 bindingsetVariablesUI 线程.因为设置变量不会触及 View 本身.

Also... don't forget that scheduling of executing changes (for the nearest future) is the thing that allows you to setVariables on binding from threads different than UI thread. Because setting a variable does not touch the View itself.

编辑 2:

查看生成的 java 类的最简单方法是:

The easiest way to look at the generated java classes is to:

  1. 导航 -> 文件

  1. 在大驼峰大写中键入绑定文件的名称,然后是 Binding(例如,如果您的布局是 activity_main,请键入 ActivityMainBinding)
  1. Type the name of your binding file in UpperCamelCase followed by Binding (e.g. if your layout is activity_main, type ActivityMainBinding)

这篇关于Android 为什么在 RecyclerView 中使用 executePendingBindings的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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