svarog
svarog

Reputation: 696

Android data binding and included layout doesn't update visibility

Hi I have made layout with couple of ImageView and TextView elements due to reduction of code duplication. I have occurred to two problems:

  1. I need to update these elements inside of layout using data binding, but I don't know how can I access to them in order to update images and texts?
  2. The visibility of layout should be bind from VM object(default visibility is GONE) but it doesn't work. Even if I update VM setter for visibility, the layout is till visible on the screen.

The code:

included layout:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:id="@+id/details"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FF0000">

        <ImageView
            android:id="@+id/image_details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/text_details"
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/favorites"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#00FF00">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/record"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#0000FF">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>

    <LinearLayout
        android:id="@+id/remind_me"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#FF0000">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />
    </LinearLayout>

    <LinearLayout
        android:id="@+id/return_to_begin"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:background="#00FF00">

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

        <TextView
            android:text="details"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

activity layout(this is a snippet, the whole xml is too large)

<include layout="@layout/manu_layout"
            android:layout_above="@+id/info"
            android:layout_alignParentRight="true"
            android:layout_height="wrap_content"
            android:layout_width="wrap_content"
            android:visibility="@{vm.infoMenu}"/>

viewModel method for visibility:

 public void setInfoMenu() {
        if(menuVisibility == View.VISIBLE){
            menuVisibility = View.GONE;
            setMediaControlView(false);
        }else {
            menuVisibility=View.VISIBLE;
            setMediaControlView(true);

        }
        notifyPropertyChanged(BR.infoMenu);
    }
    @Bindable
    public int getInfoMenu(){
        return menuVisibility;
    }

Please let me know if you need any additional code snippet.

Can you please help me to solve these problems?

Thanks,

Hogar

Upvotes: 1

Views: 1661

Answers (1)

Ravi
Ravi

Reputation: 35589

Make sure you have <layout> as a root tag of your xml file if you want to use DataBinding.

You can have a id of your <include> and acceess controls of included layout via it. For example you have a id as 'included' of your <include> then you can access it as binding.included.details or binding.included.favourite

Upvotes: 1

Related Questions