Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
DataBinding with <include> tags
public class DataBindingAdapters {
@BindingAdapter("visible") public static void setVisibility(View view, boolean isVisible) {
view.setVisibility(isVisible ? VISIBLE : GONE);
}
}
<?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>
<variable
name="viewModel"
type="io.oldering.demo.mvirxdemo.users.UsersViewModel"
/>
</data>
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="io.oldering.demo.mvirxdemo.users.UsersActivity"
>
<include
layout="@layout/user"
android:id="@+id/user"
app:visible="@{viewModel.isContentful"
app:user="@{viewModel.user}"
tools:visibility="visible"
/>
</android.support.constraint.ConstraintLayout>
</layout>

First problem

Auto completion for ConstraintLayout's contraints would not work (nothing shows up) while writing them inside the <include> tag.

Second problem

Data Binding's code generation would not use properly the binding adapter. i.e. instead of will fail calling path.util.DataBindingAdapters.setVisibility(this.user, viewModelIsContentful);, the generated code is this.user.setVisible(viewModelIsContentful); which does not exist as this.user is an instance of UserBinding and I did not set any variable named visible.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment