Android数据绑定库有任何重大错误或问题吗?

mag*_*pcu 9 java data-binding android

任何使用android数据绑定库(com.android.databinding:dataBinder)的人都能评论这个beta库吗?在android开发者网站上说"它可能包含错误,它可能不适用于你的用例,所以使用它需要你自担风险.",那么任何问题或重大错误或错误呢?

Rob*_*key 5

在过去的几周里,我一直在玩数据绑定库,考虑到它是第一个版本,它非常强大.

到目前为止,我发现的唯一错误有一个解决方法.我将在下面解释.

当使用<include>在数据绑定布局文件标签(现在使用的布局文件<layout>标记为根),生成的代码创建了一个绑定<include>标签的父ViewGroup中.

使用DataBindingUtil扩展视图时,应用程序将在尝试解析ViewGroup时崩溃.代码生成器和运行时绑定逻辑之间似乎存在不同的行为.

问题示例

这是一个带有上述问题的示例布局.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
    </data>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- This include causes no issues -->
        <include
            layout="@layout/view_content"/>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent">

            <!-- This include however causes the data binding to crash on the ScrollView -->
            <include
                layout="@layout/view_content"/>

        </ScrollView>

    </RelativeLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

这是包含的布局.

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
    </data>

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Test"/>
</layout>
Run Code Online (Sandbox Code Playgroud)

尝试使用DataBindingUtil.setContentView时,会发生以下崩溃.

java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.ScrollView.setTag(java.lang.Object)' on a null object reference
Run Code Online (Sandbox Code Playgroud)

解决方案(解决方法)

我发现的临时解决方法是将虚拟值绑定到<include>标记的父ViewGroup.这允许数据仓在运行时找到ViewGroup,避免崩溃.

以下是修复操作的示例:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:bind="http://schemas.android.com/apk/res-auto">

    <data>
        <variable
            name="viewModel"
            type="com.example.ViewModel"/>
    </data>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent">

        <!-- This include causes no issues -->
        <include
            layout="@layout/view_content"/>

        <ScrollView
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            bind:visibility="@{viewModel.dummyVisibility}">

            <!-- This include will not cause a problem now that the ScrollView has a value being bound -->
            <include
                layout="@layout/view_content"/>

        </ScrollView>

    </RelativeLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)

这是最基本的视图模型:

package com.example;

import android.databinding.BaseObservable;
import android.databinding.Bindable;
import android.view.View;

public class ViewModel extends BaseObservable {
    @Bindable
    public int getDummyVisibility() {
        // TODO: This is a work around. Currently data binding crashes on certain views if they don't have binding.
        return View.VISIBLE;
    }
}
Run Code Online (Sandbox Code Playgroud)

希望将来修复此问题并且不需要此解决方法!

编辑

我发现了另一个关于自定义绑定适配器的问题,我在https://code.google.com/p/android-developer-preview/issues/detail?id=2421上提出了这个问题.