Mur*_*ish 3 android android-layout android-databinding
我刚刚开始在 android 中处理数据绑定。理想情况下,我希望有一个包含一些通用 xml 元素的根 xml 文件,然后其内部内容可以是三个 xml 文件之一
<data>
<variable name="action" type="com.example.android.action"/>
</data>
<TextView>
<TextView>
<!--Only show one of these includes based on the binding data-->
<!-- if action.item -->
<include layout="item.xml"
bind:item="@{action.item}">
<!-- else if action.udpate -->
<include layout="update.xml"
bind:update="@{action.update}">
<!-- else if action.video -->
<include layout="video.xml"
bind:video="@{action.video}">
<TextView>
... etc
Run Code Online (Sandbox Code Playgroud)
所以基本上基于动作内部存在的子对象(项目、更新或视频)显示其布局并绑定视图但不显示其他包含。我应该只使用 android 的View:Visibility还是有我忽略的包含的东西?
我应该只使用 android 的 View:Visibility
是的,最好的方法是检查boolean数据绑定布局并相应地设置可见性,如下所示。
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
>
<data>
<import type="android.view.View"/>
<variable
name="action"
type="com.example.android.action"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/item"
android:visibility="@{action.item ? View.VISIBLE: View.GONE}"/>
<include
layout="@layout/update"
android:visibility="@{action.someOtherObject!=null ? View.VISIBLE: View.GONE}"/>
</LinearLayout>
</layout>
Run Code Online (Sandbox Code Playgroud)
在这里你可以检查NULL或Boolean如上。
| 归档时间: |
|
| 查看次数: |
5208 次 |
| 最近记录: |