【问题标题】:How to pass data variable to included layout?如何将数据变量传递给包含的布局?
【发布时间】:2020-02-01 12:32:47
【问题描述】:

有没有办法将数据变量传递给包含的布局?

父布局

<?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
        }

【问题讨论】:

    标签: android viewmodel android-databinding


    【解决方案1】:

    好吧,在本文开头我已经说过,这取决于是否可以将变量传递给辅助布局。原因如下: 数据绑定不支持包含作为合并元素的直接子元素。例如,不支持以下布局:

    <?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>
    

    【讨论】:

    • 支持什么?
    • 标签包含一个 标签,它与我们在主布局中用于将数据传递给辅助布局的名称相同:辅助用户。此外,它具有在主布局中声明的相同类型的变量。
    【解决方案2】:

    在父 xmlinclude 标记中,使用 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教程

    【讨论】:

      【解决方案3】:

      打开 build.grade 并在块 android{ // ...} 中添加 dataBinding { enabled = true }

      【讨论】:

      • 数据绑定已经开始工作了。请先阅读问题
      • 我认为数据绑定尚未起作用,因为在 build.grade 文件中看不到您的配置。对不起:(
      猜你喜欢
      • 1970-01-01
      • 2011-08-21
      • 2011-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-06-15
      • 2021-11-21
      相关资源
      最近更新 更多