我一直在开发具有数据绑定(bind)和 MVVM 的应用。
我正在尝试为横向模式下的应用使用替代布局。我有:
layout/fragment_content.xml
layout-land/fragment_content.xml
两种布局都有相同的 View 但外观不同,并从相同的 View 模型获取提要,如下所示:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data class="MyBinding">
<variable
name="viewModel"
type="com.myapp.package.viewModel.VMFirst"/>
<variable
name="controlModel"
type="com.myapp.package.viewModel.VMSecond"/>
</data>
<DIFFERENT CONTENT HERE>
所有 View 和 ID 都存在于两种布局中。
嗯,问题是,它没有编译,错误只是“找不到符号方法 getViewModel”
和其他变量的 getter。
到目前为止我尝试了什么:
使用 layout 和 layout-land 文件夹(失败,上面解释了错误)
使用布局别名 Use Layout Aliases我在这里找到的 Issue 199344: Data binding does not work with layout aliases .在尝试这种方法时,我没有更改 xml 文件中的任何内容。这也失败了,错误是
Could not write to com.myapp.package.databinding.MyBinding
是否不能在多个布局文件中使用数据绑定(bind) data
标签?在使用数据绑定(bind)时,我应该使用什么来为不同的状态使用不同的布局?谢谢!
编辑:删除 class="MyBinding"
没有改变错误。
最佳答案
如果有人搜索这个问题,2 年后我尝试做同样的事情,我发现它现在工作正常。
我在layout
和layout_sw600dp
下创建了一个布局文件activity_main
。这是 layout
资源下的布局:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<variable
name="small_variable"
type="Integer"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/myRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:id="@+id/small_square"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
这是layout_sw600dp
文件夹下的布局:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<variable
name="big_variable"
type="Long"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/myRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<View
android:id="@+id/big_square"
android:layout_width="60dp"
android:layout_height="60dp"
android:background="@android:color/holo_blue_bright"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
两者都有一个 View ,但每个都有不同的 id:small_square
和 big_square
。
我在手机和平板电脑上运行该项目。以下是我的发现:
- DataBinding 创建一个实现,它包含不同布局文件夹中所有同名布局文件下的所有 View 和变量。
- 存在于所有布局中的 View 都不能为空,所有其他 View 都可以为空。在上面的 XML 中,当使用来自 Kotlin 的绑定(bind)时,
myRoot
是不可空 View ,而big_square
和small_square
可空 View 。变量可以为空,无论它们是否存在于所有布局中(这是预期的行为)。 - 您不能在每个文件中命名不同的绑定(bind)类。它必须相同(上面示例中的
MainBinding
,或者如果您未定义它,则默认为LayoutResourceName
+Binding
)。 - 关于绑定(bind)实现的 View 和变量的名称是驼峰式的。所以我的
small_variable
和small_square
在代码端是binding.smallVariable
和binding.smallSquare
。 - 使用 Kotlin,您可以只使用像
binding.bigSquare?.operation
这样的 View ,这很棒,您无需事先检查它是平板电脑还是手机或 View 是否为 null。 - 提示,您可以分配
binding
字段,即使它们所在的布局不会被使用。您仍然可以在代码上说binding.smallVariable = 3
,它会进行赋值并保存值。我认为小心点是好的。
关于Android DataBinding & MVVM - 对不同布局文件夹中的布局文件使用相同的名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39364813/