web-dev-qa-db-ja.com

データリンク内のインクルードレイアウトの可視性を設定する方法

プロジェクトにデータバインディングを実装しました。インクルードタグにネストされたレイアウトが2つの特定の画面があります。プログラムでデータバインディングを使用して、インクルードレイアウトの可視性を変更できませんでした。

しかし、私はブール値を通してそれを達成しましたが、私の質問はプログラム的にタグを含む可視性を設定する方法です。

私の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"/>
 _

そしてアクティビティ:これを設定しようとすると - それは赤くなることを意味します。

  dataBinding.regPhone.setVisibility(View.GONE);
  dataBinding.regEmail.setVisibility(View.VISIBLE);
 _

より良い方法.

トップレイアウトで、BooleanまたはObservationalフィールドを宣言します。値が含まれているレイアウトの表示を切り替える。それから含まれているレイアウトを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>
 _
0
Edijae Crusar