1.visibility的使用错误
<ProgressBar
android:layout_width="0dp"
android:layout_height="25dp"
android:layout_weight="1"
android:layout_gravity="center"
android:visibility="@{!viewModel.isLoaded.get}"/>
其中的
android:visibility="@{!viewModel.isLoaded.get}"应该修改为
android:visibility="@{!viewModel.isLoaded.get ? View.VISIBLE : View.GONE}"
2.使用view而未引用view
添加一行import,引入View进来:
3.xml中data标签只是绑定model的声名,切记要在代码中进行数据绑定:
首先在xml内
然后是在代码内设置数据进行数据绑定
4.可以在表达式中直接引用带 id 的 view,引用时采用驼峰命名法。
<TextView
android:id="@+id/first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={user.firstName}" />
<TextView
android:text="@{user.lastName}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="@{firstName.getVisibility() == View.GONE ? View.GONE : View.VISIBLE}" />
<!-- 这里TextView直接引用第一次TextView,firstName为id 的驼峰命名 -->
5.在使用应用命名空间的布局中,变量可以传递到任何 include 布局中,并有两种方法
分别是bind和app
在include内
在include的引用文件中接收
6.在xml中使用DataBinding拼接字符串时,直接用中文拼接
报错:
Error:Execution failed for task ‘:app:compileDebugJavaWithJavac’.
android.databinding.tool.util.LoggedErrorException: failure, see logs for details.
Exception while handling step android.databinding.annotationprocessor.ProcessExpressions@7cf285d5 javax.xml.bind.UnmarshalException- with linked exception:
[org.apache.xerces.impl.io.MalformedByteSequenceException: Invalid byte 3 of 3-byte UTF-8 sequence.]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.createUnmarshalException(AbstractUnmarshallerImpl.java:333)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.createUnmarshalException(UnmarshallerImpl.java:563)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:249)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:214)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:157)
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.unmarshal(AbstractUnmarshallerImpl.java:204)
at android.databinding.tool.store.ResourceBundle
只需要将中文写在string.xml中,从string.xml中获取中文来拼接就行!!!(注:只要不是直接用中文拼接就行,其他方式如将中文写成变量引入,也是可以的!!!)