Android 为什么/何时使用 ExecutePendingBindings [英] Android Why/ when to use ExecutePendingBindings

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

问题描述

最近我一直在使用数据绑定,我遇到了 executePendingBindings 方法.

Lately I've been using data bindings and I came across the executePendingBindings method.

文档并没有太多说明,所以我不明白它是如何工作的或何时使用它.以下是该方法的用法示例.

The documentation doesn't say much about it, so I don't understand how it works or when to use it. Here is an example of the method's usage.

请举例说明使用和不使用的区别.谢谢

Please give an example that demonstrates the difference of using it and not. Thank you

@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();
    
}

推荐答案

更详细的解释可以参考这个例子.

For a more detailed explanation you can refer to this example.

当不使用 executePendingBindings() 时,您的列表在绑定时会闪烁,例如,当您打开一个新列表时,您会注意到这种闪烁/抖动效果,这是因为列表已填充并且然后在下一帧它被绑定.

When not using executePendingBindings() your list will flicker when you bind it, for example when you open a new list you will notice this flicker / jitter effect, this is because the list is populated and then in the next frame it's bound.

如果您不希望这种情况发生并立即执行,您绑定此方法将防止闪烁并立即绑定您的数据.

If you don't want this to happen and execute immediately, your binding this method will prevent the flickering and bind your data right away.

如果你看到了,有一个闪烁的效果,因为视图被初始化,然后数据绑定,然后在下一帧发生视图绑定.

If you see, there is a flickering effect since the views are initialized, then data binded and then in the next frame the view binding happens.

如果看到这里,没有闪烁的效果,可以看到列表绑定正常,可以一次性使用.

If you see here, there is no flickering effect, you can see that the list is binded normally and works as one go.

您只能在 UI 线程上使用 executePendingBindings(),这意味着在调用 onBindViewHolder 时,您将需要在绑定上使用它,做so 将保证您在 UI 上调用它.

You can only use executePendingBindings() on the UI thread, this means that in the time onBindViewHolder is called, you will need to use it on the binding, doing so will guarantee that you are calling it on the UI.

override fun onBindViewHolder(binding: MyBindingClass, position: Int, viewType: Int) {
        //Your binding code
        binding.executePendingBindings()
    }

总是在 onBindViewHolder 的末尾调用它

Always call it at the end of the onBindViewHolder

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

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