Skip to content

Instantly share code, notes, and snippets.

@oldergod
Last active March 30, 2017 06:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save oldergod/00aeaebc09d41492963275069b60add2 to your computer and use it in GitHub Desktop.
Save oldergod/00aeaebc09d41492963275069b60add2 to your computer and use it in GitHub Desktop.
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