Ant*_*adz 4 android viewmodel android-databinding
有没有办法将数据变量传递给包含的布局?
父布局
<?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>
Run Code Online (Sandbox Code Playgroud)
并希望在包含的布局中包含 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>
...........
Run Code Online (Sandbox Code Playgroud)
还是只有在这样设置时才可能:
binding = FragmentSettingsBinding.inflate(inflater, container, false).apply {
lifecycleOwner = this@SettingsFragment
viewModel = this@SettingsFragment.viewModel
main.viewModel = this@SettingsFragment.viewModel
}
Run Code Online (Sandbox Code Playgroud)
在母体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>
Run Code Online (Sandbox Code Playgroud)
您包含的布局将在viewModel.
有关更多详细信息,请查看本教程
| 归档时间: |
|
| 查看次数: |
2759 次 |
| 最近记录: |