将变量传递到 <include> 的布局标签

Passing a variable into <include> 's layout tag

你好,我想创建一个基础 xml,它本身可以容纳可变布局。这是基本思想:

基本布局:

<data>
    <variable name="view" type="android.support.annotation.DrawableRes">
    <variable name="viewModel" type="myBaseViewModel">
</data>

<RelativeLayout> 

    <include layout="@{view}"
        bind:viewModel=@{viewModel}>

</RelativeLayout>

项目布局:

<layout> 
    <data>
        <variable name="viewModel" type="myBaseViewModel"/>
    </data>

    <TextView text = "@{viewModel.myText}" />
</layout>

活页夹布局:

<layout>
    <data>
        <variable name="viewModel" type="myViewModel"/>
    </data>

    <include layout="@layout/myBaseLayout"
        bind:view="@layout/myItemLayout"
        bind:viewModel="@{viewModel}/>
</layout>

有什么办法可以实现吗?

我想您也希望此字段随变量一起更新。不,这是不可能的。 您必须在后台获取此字段引用并在每次更新变量时更新它。