假设我有以下布局文件:
first_layout.xml
<layout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First layout title"/>
<include
layout="@layout/reusable_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
second_layout.xml
<layout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second layout title"/>
<include
layout="@layout/reusable_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</layout>
reusable_layout.xml
<layout>
<TextView
android:id="@+id/text_to_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="replace me"/>
</layout>
...并且我有两个不同的 ViewModel(FirstLayoutViewModel 和 SecondLayoutViewModel,每个都有不同的 String 属性)。我想使用数据绑定(bind)将每个 ViewModel 的字符串属性绑定(bind)到 text_to_replace
reusable_layout.xml
中的元素.
我该怎么做?通常,我会在<data>
中声明一个变量。标记并将类型分配给适当的 ViewModel,但是如果多次使用布局 ( reusable_layout.xml
),我该如何执行此操作?它如何知道要使用哪种 ViewModel 类型?
在这个例子中,我可以为first_layout和second_layout布局使用不同的TextView,而不是导入布局,但在我的实际项目中,我想要重用的布局要复杂得多,重用是有意义的。
任何帮助都会很棒。谢谢!
最佳答案
你是说你只需要 a 来显示 String
所以不要传递 FirstLayoutViewModel
和SecondLayoutViewModel
,您应该通过 String
给您reusable_layout
.
<layout>
<data>
<variable name="myText" type="String> />
</data>
<TextView
android:id="@+id/text_to_replace"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{myText}"/>
</layout>
更多详情: how to pass data in includes
class MyViewModel1 : ViewModel {
private val _myLiveData = MutableLiveData<String>()
val myLiveData: LiveData<String>
get() = _myLiveData
init {
//Assign MyLiveData
}
}
class MyViewModel2 : ViewModel {
private val _mySecondLiveData = MutableLiveData<String>()
val mySecondLiveData: LiveData<String>
get() = _mySecondLiveData
init {
//Assign MyLiveData
}
}
第一个布局:
<layout>
<data>
<variable name="viewModel" type="MyViewModel1"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First layout title"/>
<include
layout="@layout/reusable_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:myText="@{viewModel.myLiveData}"
/>
</LinearLayout>
</layout>
第二布局:
<layout>
<data>
<variable name="viewModel" type="MyViewModel2"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="@+id/title_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First layout title"/>
<include
layout="@layout/reusable_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:myText="@{viewModel.mySecondLiveData}"
/>
</LinearLayout>
</layout>
关于android - 如何将数据绑定(bind)与重用布局结合使用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62676482/