android - DataBindingUtil findBinding(查看 View )与 getBinding(查看 View )

标签 android data-binding android-databinding

该方法的预期目的是什么 findBinding(View view)

我一直在使用数据绑定(bind)库测试版。
目前还没有针对各个类的官方引用文档,所以我一直在查看源代码以了解我们可以访问哪些方法。

DataBindingUtil 类有两个方法,听起来它们会做类似的事情:

  • public static <T extends ViewDataBinding> T findBinding(View view)
  • public static <T extends ViewDataBinding> T getBinding(View view)

第二种方法getBinding只是一个调用 ViewDataBinding 的实用方法类' getBinding方法。

第一种方法findBinding更不清楚的是要遵循和确定其目的。有什么想法吗?

最佳答案

抱歉,网页中缺少文档,但它们应该在 sdk maven 存储库中。

不同之处在于,如果给定 View 不是绑定(bind)根,则 find 绑定(bind)将遍历父级,而 getBinding 将返回 null。以下是文档:

/**
 * Retrieves the binding responsible for the given View. If <code>view</code> is not a
 * binding layout root, its parents will be searched for the binding. If there is no binding,
 * <code>null</code> will be returned.
 * <p>
 * This differs from {@link #getBinding(View)} in that findBinding takes any view in the
 * layout and searches for the binding associated with the root. <code>getBinding</code>
 * takes only the root view.
 *
 * @param view A <code>View</code> in the bound layout.
 * @return The ViewDataBinding associated with the given view or <code>null</code> if
 * view is not part of a bound layout.
 */
public static <T extends ViewDataBinding> T findBinding(View view) {

/**
 * Retrieves the binding responsible for the given View layout root. If there is no binding,
 * <code>null</code> will be returned. This uses the DataBindingComponent set in
 * {@link #setDefaultComponent(DataBindingComponent)}.
 *
 * @param view The root <code>View</code> in the layout with binding.
 * @return The ViewDataBinding associated with the given view or <code>null</code> if
 * either the view is not a root View for a layout or view hasn't been bound.
 */
public static <T extends ViewDataBinding> T getBinding(View view) {

关于android - DataBindingUtil findBinding(查看 View )与 getBinding(查看 View ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31579777/

相关文章:

android - 替换 .mp4 文件的音轨

android - 切换到主页(到启动器)

asp.net-mvc - 为什么 ASP.NET MVC 默认 Model Binder 很慢?它需要很长时间才能完成工作

kotlin - 数据绑定(bind)失败,编译期间未找到 JAXB 类异常(使用 kapt)

使用数据绑定(bind)的具有多个 View 类型的 Android recyclerview 适配器

android - 在 MVVM 应用程序中公开 XML 布局中的 Activity 是一种不好的做法吗?

android - Qt Android : Why is a QtApp-debug. apk 为发布版本创建?

data-binding - INotifyCollectionChanged 不足以更新数据绑定(bind)控件?

c# - WPF 嵌套数据绑定(bind)到控件 - 为什么它不起作用

android - 将 4 uchar 装箱为 int 时出现问题(RenderScript)