如何将数据绑定与重用布局一起使用?
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,每个都有不同的字符串 属性)。我想使用数据绑定将每个 ViewModel 的字符串 属性 绑定到 reusable_layout.xml
.
中的 text_to_replace
元素
我该怎么做?通常,我会在 <data>
标记中声明一个变量并将类型分配给适当的 ViewModel,但是如果布局 (reusable_layout.xml
) 被多次使用,我该如何做呢?它如何知道要使用哪种 ViewModel 类型?
在这个例子中,我可以为 first_layout 和 second_layout 布局使用不同的 TextView 而不是导入布局,但在我的真实项目中,我想重用的布局要多得多复杂,重用很有意义。
任何帮助都会很棒。谢谢!
您是说您只需要一个来显示 String
,因此您应该将 String
传递给您的 [,而不是传递 FirstLayoutViewModel
和 SecondLayoutViewModel
=18=].
<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
}
}
FirstLayout:
<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>
假设我有以下布局文件:
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,每个都有不同的字符串 属性)。我想使用数据绑定将每个 ViewModel 的字符串 属性 绑定到 reusable_layout.xml
.
text_to_replace
元素
我该怎么做?通常,我会在 <data>
标记中声明一个变量并将类型分配给适当的 ViewModel,但是如果布局 (reusable_layout.xml
) 被多次使用,我该如何做呢?它如何知道要使用哪种 ViewModel 类型?
在这个例子中,我可以为 first_layout 和 second_layout 布局使用不同的 TextView 而不是导入布局,但在我的真实项目中,我想重用的布局要多得多复杂,重用很有意义。
任何帮助都会很棒。谢谢!
您是说您只需要一个来显示 String
,因此您应该将 String
传递给您的 [,而不是传递 FirstLayoutViewModel
和 SecondLayoutViewModel
=18=].
<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
}
}
FirstLayout:
<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>