[英]How do I use Data Binding with reused layouts?
假设我有以下布局文件:
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 属性)。 我想使用数据绑定将每个 ViewModel 的字符串属性绑定到reusable_layout.xml
中的text_to_replace
元素。
我怎样才能做到这一点? 通常,我会在<data>
标记中声明一个变量并将类型分配给适当的 ViewModel,但是如果多次使用布局( reusable_layout.xml
),我该怎么做? 它如何知道要使用哪种 ViewModel 类型?
在这个例子中,我可以为 first_layout 和 second_layout 布局使用不同的 TextView 而不是导入布局,但在我的实际项目中,我想要重用的布局要复杂得多,重用是有意义的。
任何帮助都会很棒。 谢谢!
你是说你只需要 a 来显示一个String
所以你应该将String
传递给你的reusable_layout
,而不是传递FirstLayoutViewModel
和SecondLayoutViewModel
。
<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>
更多细节: 如何在包含中传递数据
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>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.