home.

tagged: android-databinding

Android: Databinding and ViewPagers

Once you have a data binding, you will want to use it with a ViewPager eventually.
 
The ViewPager inflates layouts. But if you want to inflate layouts with the <data></data> section under a <layout> tag, you must change your layout inflation code.
 
In instantiateItem():

YourLayout layout = DataBindingUtil.inflate(LayoutInflater.from(MainActivity.this), "R.layout.your_layout", container, false);
// Set your databinding up here
container.addView(layout.getRoot());
return layout.getRoot();

The getRoot() method is there because data binding won't return the actual layout, but the binding. But getRoot() will give you the layout.
 
If you have dynamic layouts, i.e. it's not always "R.layout.your_layout", then you won't always return YourLayout. You can instead return a ViewDataBinding.
 
But the problem with the above is now you can't bind variables to that Binding, instead you'll have to cast it to the appropriate generated class:

if(layouts[position]==R.layout.some_layout) {
  SomeLayoutBinding slb = DataBindingUtil.bind(db.getRoot());
  slb.setThing("Hoooo!");
}

android android-databinding android-viewpager


Android Databinding basics

Although a large topic, databinding is simple to set up. From Android Studio 2.0, in your app's AndroidManifest.xml:

android {
  ...
  dataBinding {
      enabled = true
  }
  ...
}

Then change your layout file, main_activity.xml for the most basic version, to something like:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
  <data>
    <variable
        name="thingvar"
        type="com.example.blar.myapplication.Thing"
        />
  </data>
  ...
</layout>

The data section defines variables, and specify a type. In our case, the Thing class is POJO:

public class Thing {
  private String thing;

  public Thing(String thing) {
    this.thing = thing;
  }

  public String getThing() {
    return thing;
  }

  public void setThing(String thing) {
    this.thing = thing;
  }
}

To bind a property, in an element, TextView for example, set the attribute android:text=@{thingvar.thing}.
 
Now in your Activity, instead of setContentView(), use the follow binding methods:

Thing test = new Thing("Test");
final MainActivityBinding binding = DataBindingUtil.setContentView(this, R.layout.main_activity);
binding.setThing(test);

The MainActivityBinding class is autogenerating based on your layout file name, main_activity in this case.
 
Not only does setting the Thing class on the binding update the TextView we mentioned, but you can also avoid findViewById() call. They're now available via binding.theIDofYourElement.

android android-databinding

Page 2 of 2
prev