DataBindingUtil.findBinding(View view)和getBinding(View view)有哪些区别?

3
方法findBinding(View view)的预期目的是什么? 我一直在使用Data Binding Library beta版本。由于没有关于各个类的官方参考文档,所以我一直在查看源代码,看看我们可以访问哪些方法。 DataBindingUtil类有两个听起来似乎会做类似事情的方法:
  • public static <T extends ViewDataBinding> T findBinding(View view)
  • public static <T extends ViewDataBinding> T getBinding(View view)
第二种方法getBinding只是一个实用方法,调用了ViewDataBinding类的getBinding方法。
第一个方法findBinding有点不太清楚,难以理解其目的。你有什么想法吗?
1个回答

6

抱歉,该网页缺少说明文档,但应在 SDK Maven 存储库中提供。

区别在于,find binding 将遍历父级,而 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) {

2
已经有一段时间了,但是没有适用于数据绑定的正确文档!我看到许多在Medium和G+上关于数据绑定的博客文章。您应该将其添加到文档中,而不是先发布博客。 - kirtan403

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接