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

查看:1155
本文介绍了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...

也...别忘了(在最近的将来)安排执行更改的时间表,这使您可以从与UI线程不同的线程在binding上的setVariables上进行操作.因为设置变量不会触及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.

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

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

  1. 导航->文件

  1. 在UpperCamelCase中键入绑定文件的名称,后跟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天全站免登陆