[JAVA] Android Studio data binding study summary

First, the XML layout

:qiita.rb
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"/>
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.lastName}"/>
   </LinearLayout>
</layout>

What I would like to pay attention to is this definition

<variable name="user" type="com.example.User" />

When,

<TextView android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:text="@{user.firstName}" />

The call part from this ViewModel.

And next seems to be an entity.

public class User {
  private final String firstName;
  private final String lastName;
  public User(String firstName, String lastName) {
      this.firstName = firstName;
      this.lastName = lastName;
  }
  public String getFirstName() {
      return this.firstName;
  }
  public String getLastName() {
      return this.lastName;
  }
}

The expression @ {user.firstName} used for the text attribute accesses the firstName field and the getFirstName () method of the latter class.

Now, it looks like the Activity that connects xml and the entity.


@Override
protected void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   ActivityMainBinding binding = DataBindingUtil.setContentView(this, R.layout.activity_main);
   User user = new User("Test", "User");
   binding.setUser(user);
}

The above is like Activity, it seems that binding is instantiated and xml and User entity are linked.

Also, the binding expression seems to be able to assign the view's click listener to the onClickFriend () method, like this: ..

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
   <data>
       <variable name="handlers" type="com.example.MyHandlers"/>
       <variable name="user" type="com.example.User"/>
   </data>
   <LinearLayout
       android:orientation="vertical"
       android:layout_width="match_parent"
       android:layout_height="match_parent">
       <TextView android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:text="@{user.firstName}"
           android:onClick="@{handlers::onClickFriend}"/>
   </LinearLayout>
</layout>

And it reacts to the following code


public class MyHandlers {
    public void onClickFriend(View view) { ... }
}

In the case of this onSaveClick,


public class Presenter {
    public void onSaveClick(Task task){}
}

It looks like this:


<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent">
        <Button android:layout_width="wrap_content" android:layout_height="wrap_content"
        android:onClick="@{() -> presenter.onSaveClick(task)}" />
    </LinearLayout>

Recommended Posts

Android Studio data binding study summary
[Android / Kotlin] UI summary
Android development link summary
[Android] Variable of Data Binding is written in camelCase
Notes in Android studio
Defeating Android Studio Part 3-6
Defeating Android Studio Part 1 & Part 2
RxJava (RxAndroid) + RxBus + Data Binding + room sample (based on Java1.7) on Android.