如何将数据变量传递给包含的布局?
How to pass data variable to included layout?
有没有办法将数据变量传递给包含的布局?
父布局
<?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"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:overScrollMode="never"
android:padding="@dimen/spacing_default">
<include
android:id="@+id/main"
layout="@layout/layout_settings_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
并希望在包含的布局中包含 viewModel
<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"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<merge>
...........
或者只有这样设置才可以:
binding = FragmentSettingsBinding.inflate(inflater, container, false).apply {
lifecycleOwner = this@SettingsFragment
viewModel = this@SettingsFragment.viewModel
main.viewModel = this@SettingsFragment.viewModel
}
好的,在本文的开头我已经说过,这取决于是否可以将变量传递给辅助布局。原因如下:
数据绑定不支持将 include 作为合并元素的直接子元素。例如,不支持以下布局:
<?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="user" type="it.communikein.myunimib.User"/>
</data>
<merge>
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</merge>
</layout>
在父 xml
中 include
标签中使用 bind:viewModel
.
将数据变量传递到包含的布局
<?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"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:overScrollMode="never"
android:padding="@dimen/spacing_default">
<include
android:id="@+id/main"
layout="@layout/layout_settings_main"
bind:viewModel="@{viewModel}" />
</androidx.constraintlayout.widget.ConstraintLayout>
您包含的布局将在 viewModel
中获得对象实例。
有关详细信息,请查看 this 教程
打开 build.grade 并在块中添加 dataBinding { enabled = true } android{ // ...}
有没有办法将数据变量传递给包含的布局?
父布局
<?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"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:overScrollMode="never"
android:padding="@dimen/spacing_default">
<include
android:id="@+id/main"
layout="@layout/layout_settings_main" />
</androidx.constraintlayout.widget.ConstraintLayout>
并希望在包含的布局中包含 viewModel
<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"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<merge>
...........
或者只有这样设置才可以:
binding = FragmentSettingsBinding.inflate(inflater, container, false).apply {
lifecycleOwner = this@SettingsFragment
viewModel = this@SettingsFragment.viewModel
main.viewModel = this@SettingsFragment.viewModel
}
好的,在本文的开头我已经说过,这取决于是否可以将变量传递给辅助布局。原因如下: 数据绑定不支持将 include 作为合并元素的直接子元素。例如,不支持以下布局:
<?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="user" type="it.communikein.myunimib.User"/>
</data>
<merge>
<include layout="@layout/name"
bind:user="@{user}"/>
<include layout="@layout/contact"
bind:user="@{user}"/>
</merge>
</layout>
在父 xml
中 include
标签中使用 bind:viewModel
.
<?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"
xmlns:bind="http://schemas.android.com/apk/res-auto"
tools:showIn="@layout/fragment_settings">
<data>
<variable
name="viewModel"
type="......settings.SettingsFragmentViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:overScrollMode="never"
android:padding="@dimen/spacing_default">
<include
android:id="@+id/main"
layout="@layout/layout_settings_main"
bind:viewModel="@{viewModel}" />
</androidx.constraintlayout.widget.ConstraintLayout>
您包含的布局将在 viewModel
中获得对象实例。
有关详细信息,请查看 this 教程
打开 build.grade 并在块中添加 dataBinding { enabled = true } android{ // ...}