如何设置数据绑定中包含布局的可见性?
How to set visibility for include layout in databinding?
我在我的项目中实现了数据绑定。我有一个特定的屏幕,在 include 标签中有两个嵌套布局。我无法以编程方式使用数据绑定更改包含布局的可见性。
不过,我是通过布尔值实现的,但我的问题是如何以编程方式设置该包含标记的可见性。
我的xml:
<include
android:id="@+id/reg_email"
layout="@layout/custom_email"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include
android:id="@+id/reg_phone"
layout="@layout/custom_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在 Activity 中:
当我尝试设置它时 - 它变成红色意味着它不会将其视为视图。
dataBinding.regPhone.setVisibility(View.GONE);
dataBinding.regEmail.setVisibility(View.VISIBLE);
将 get root 添加到您的视图中
dataBinding.regPhone.getRoot().setVisibility(View.GONE);
dataBinding.regEmail.getRoot().setVisibility(View.VISIBLE);
更好的方法。
在顶部布局中,声明布尔值或可观察字段,其值可切换所含布局的可见性。然后记得给包含的布局一个 id
否则它不会工作
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<variable
name="show"
type="Boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="@color/colorPrimary">
<include layout="@layout/progress"
android:id="@+id/progress"
android:visibility="@{show?View.VISIBLE:View.GONE}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
创建具有参考视图 ID 的组对我有用,因为我使用 <merge>
标签包含布局。
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="emptyStateView,emptyStateDescription"
android:visibility="@{viewModel.searchResultState.result.size() == 0 ? View.VISIBLE : View.GONE}"/>
我在我的项目中实现了数据绑定。我有一个特定的屏幕,在 include 标签中有两个嵌套布局。我无法以编程方式使用数据绑定更改包含布局的可见性。
不过,我是通过布尔值实现的,但我的问题是如何以编程方式设置该包含标记的可见性。
我的xml:
<include
android:id="@+id/reg_email"
layout="@layout/custom_email"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<include
android:id="@+id/reg_phone"
layout="@layout/custom_phone"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
在 Activity 中: 当我尝试设置它时 - 它变成红色意味着它不会将其视为视图。
dataBinding.regPhone.setVisibility(View.GONE);
dataBinding.regEmail.setVisibility(View.VISIBLE);
将 get root 添加到您的视图中
dataBinding.regPhone.getRoot().setVisibility(View.GONE);
dataBinding.regEmail.getRoot().setVisibility(View.VISIBLE);
更好的方法。
在顶部布局中,声明布尔值或可观察字段,其值可切换所含布局的可见性。然后记得给包含的布局一个 id
否则它不会工作
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<data>
<import type="android.view.View"/>
<variable
name="show"
type="Boolean" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:background="@color/colorPrimary">
<include layout="@layout/progress"
android:id="@+id/progress"
android:visibility="@{show?View.VISIBLE:View.GONE}"/>
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
创建具有参考视图 ID 的组对我有用,因为我使用 <merge>
标签包含布局。
<androidx.constraintlayout.widget.Group
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="emptyStateView,emptyStateDescription"
android:visibility="@{viewModel.searchResultState.result.size() == 0 ? View.VISIBLE : View.GONE}"/>