Android Data Binding: visibility on include tag

前端 未结 9 708
旧时难觅i
旧时难觅i 2020-12-24 10:38

As per http://developer.android.com/tools/data-binding/guide.html#imports, we can have such simple expressions in visibility:



        
相关标签:
9条回答
  • 2020-12-24 11:13

    You can find the included layout by its id and hide it using the visibility attribute.

    For example in Kotlin,

    binding.root.findViewById<CardView>(R.id.layout_inclulded).visibility = View.GONE

    where,

    root is the default attribute for the root element in the parent layout.

    CardView is the root element of the included layout in my case.

    R.id.layout_inclulded is the id of the included layout in the parent layout.

    0 讨论(0)
  • 2020-12-24 11:14

    Try:

    this.imageLayout.getRoot().setVisibility(NotifTypeNotificatio1);
    
    0 讨论(0)
  • 2020-12-24 11:17

    Best Way

    1. You can directly pass Integer value of visibility like.
    2. You can set default value of visibility also by setting default=gone in binding.

    For example this is included_layout.xml

    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
    
            <variable
                name="visibility"
                type="int"/>
    
        </data>
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{visibility, default=gone}"
            />
    
    </layout>
    

    and use like

    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
    
          <import type="android.view.View"/>
    
        </data>
    
       <include
         android:id="@+id/included_layout"
         layout="@layout/included_layout"
         app:visibility="@{View.VISIBLE}"/>
    
    </layout>
    
    0 讨论(0)
  • 2020-12-24 11:17

    Thank to everybody, you help me a lot. Your answers were part of my succes on this trouble.

    We wanted to set visibility of an image view, that was also target of some animation. The Motion Scene was taking the management of the visibility. Set the motion property app:visibilityMode to "ignore" solve everything.

    <MotionScene xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
    
    <ConstraintSet android:id="@+id/expanded">
    <!-- ... -->
        <Constraint
            android:id="@+id/myImageViewId"
            app:visibilityMode="ignore"/>
    </ConstraintSet>
    <!-- ... -->
    </MotionScene>
    
    0 讨论(0)
  • 2020-12-24 11:26

    I imagine what you are trying to do would look something like this:

    In the layout you are including, specify a boolean variable and bind it to the desired view's visibility

    <layout xmlns:android="http://schemas.android.com/apk/res/android">
    
        <data>
    
            <import type="android.view.View"/>
    
            <variable
                name="isVisible"
                type="boolean"/>
    
        </data>
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{isVisible ? View.VISIBLE : View.GONE}"/>
    
    </layout>
    

    Then In your calling layout bind your value

    <include
        android:id="@+id/image_layout"
        layout="@layout/image_layout"
        bind:isVisible="@{notification.notifType == 0}"/>
    
    0 讨论(0)
  • 2020-12-24 11:26

    You are able to pass all necessary parameters from parent xml into included xml via "http://schemas.android.com/apk/res-auto" namespace.

    The syntax is res-auto namespace:variable name

    For example

    <?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">
    
        <android.support.design.widget.CoordinatorLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">
    
            <include
                layout="@layout/include_user_image"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:isVisible="@{true}" />
    
        </android.support.design.widget.CoordinatorLayout>
    </layout>
    

    include_user_image.xml

    <?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="isVisible"
                type="boolean" />
    
        </data>
    
        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:visibility="@{isVisible ? View.VISIBLE : View.GONE}" />
    </layout>
    
    0 讨论(0)
提交回复
热议问题