Android data binding DataBinding (a) Beginners

Morning saw a tweet "still using cumbersome findViewById, followed tried it DataBinding, before using ButteKnife had been blown away findViewById. Because micro letter written a small program, that in js data source changes, the data in the UI followed by change, really cool. This article personal humble opinion, if there is fraught office, please ask, grateful - trembling from small slag slag

Configuration

About DataBinding introduction is not to say, I did not get to know, first will be used.
In appthe build.gradlemiddle add the following, expressed support for data binding, (personal understanding) because the use of data binding automatically generates a lot of things through the layout file, see below.

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

Use layout

A new activity, the following layout in which

<layout>
    <data>
        <variable name="user" type="com.jiajia.mypractisedemos.module.jetpack.User"/>
    </data>

    <LinearLayout 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"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context="com.jiajia.mypractisedemos.module.jetpack.JetpackActivity">

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

        <Button
            android:layout_width="wrap_content"
            android:text="更换User"
            android:layout_height="wrap_content" />
    ......
    </LinearLayout>
</layout>

Here the root layout is not usually used in several layouts, but rather layout, see other article says use layout will automatically generate binding classes (see below).
In the new variables for data tags variable, set nameproperties and typeattributes, name easily take, in this xml in, type herein may be basic data types we usually use, may be its own package, and such like apparent that layout files require data, a User class is defined here, how common chestnut ah. User class is a simple matter of Model

public class User{
    private String userName;
    private int age;

    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }
    public String getUserName() {
        return userName == null ? "" : userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

Back to xml, copy here TextView nodes, easy to navigate

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

android.text = "@{user.userName}"Use @{}can take a value user in, the user is tied to the point at the top-defined data matter, here, I started thinking, this user is not the time I conducted tie point operation in my Activity in can be displayed directly to my interface, ah. bingo, nonsense, otherwise called data binding ah.

Activity in Binding

With the layout files, with the need to tie data points, how will it be in a bind, put it plainly, I need to use user interface object, I had to initialize it.
Very simple, in Activity, create two global variables

ActivityJetpackBinding binding;
User user;

Here user nothing to say, and that ActivityJetpackBindingis what the hell, I talk about the Activity name JetpackActivity, so, this class is in front of me automatically generated it, how to instantiate it.

binding = DataBindingUtil.setContentView(this,R.layout.activity_jetpack);

Use DataBindingUtilit, Hey, this setContentViewis so familiar, in general onCreate have this, the thought of binding, is before this sentence that would setContentViewalso kill, as I carefully removed setContentView, and she can run, how to get rid of I (in fact, do not look, I'm just curious how to get rid of, so look at the source),

Point to go see

public static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId) {
        return setContentView(activity, layoutId, sDefaultComponent);
    }

And then return back to the pointsetContentView

public static <T extends ViewDataBinding> T setContentView(Activity activity, int layoutId,
            DataBindingComponent bindingComponent) {
        activity.setContentView(layoutId);
        View decorView = activity.getWindow().getDecorView();
        ViewGroup contentView = (ViewGroup) decorView.findViewById(android.R.id.content);
        return bindToAddedViews(bindingComponent, contentView, 0, layoutId);
    }

ok, familiar activity.setContentView(layoutId);
explanation really blown away.

Was binding target, create a new User, how will the new User and binding it to link
the original binding has a setter and getter friends binding.setUser(user);, it is so run, the value of the TextView with, not used before the full setTex (), complete one o'clock the onCreate Code

//        setContentView(R.layout.activity_jetpack);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_jetpack);
        user = new User("家佳",25);
        binding.setUser(user);

Here, the basic binding is realized, be the entry of the bar, take a look at the difference between the previous and the layout file no id, activity does not bind the id, not setText, it does seem much less. But .....

This is certainly not the effect I want to, ah, all I want is to change the value of the user, the interface change accordingly, do not have to setText
So I started thinking

Think

If I change the value of the User, the interface will change along with it? In fact, certainly, but I do not know how to use my idea is very simple, very simple, put forward a Button, click on the name change of the value of the global User interface change along, ok, I wrote, but hit his face I how so cute, too young thought it would be simple.
So how can we achieve this effect yet.

Dynamic binding

The original need to work hard class User
data binding provides a base class BaseObservable, it needs to inherit, User modified as follows


public class User extends BaseObservable{
    private String userName;
    private int age;
    public User(String userName, int age) {
        this.userName = userName;
        this.age = age;
    }
    @Bindable
    public String getUserName() {
        return userName == null ? "" : userName;
    }
    public void setUserName(String userName) {
        this.userName = userName;
        notifyPropertyChanged(BR.userName);
    }
    @Bindable
    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
        notifyPropertyChanged(BR.age);
    }
}

In the above plus getter @Bindable, setter function in Canada notifyPropertyChanged, so familiar Adapter ah, a look at the observer pattern implementation should be friends. Here notifyPropertyChangedneed an int parameter, see some reportedly similar R.java the id, like, do not know, look at people with BR, BR directly to, on BR is what, do not know.
After changing the User class before the button by user.setUsername (), the perfect realization of the change in the data layout.

At last

Data Binding There are a lot of things, such as dynamic binding, without too much trouble, such as Button's onClick event can also bind, to say next.

Sincerely, salute

Guess you like

Origin www.cnblogs.com/numen-fan/p/11068358.html