current position:Home>Android DataBinding Jetpack component

Android DataBinding Jetpack component

2023-01-25 11:31:02Someone from Yungu Li

Android之DataBinding

在这里插入图片描述

DataBinding

数据绑定

亮点

  • There is no need to hold a reference to the control during development
  • Has the feature of two-way binding
  • 数据与UI同步
    在这里插入图片描述

使用DataBinding

1、gradle中添加dataBinding

android {
    
    dataBinding {
    
        enabled = true
    }
}

2、绑定xml

@Override
protected void onCreate(Bundle savedInstanceState) {
    
    super.onCreate(savedInstanceState);
    ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
    .......................
}

单向绑定数据

定义数据模型

// The first way of one-way refresh
public class User extends BaseObservable {
    

    private String name;
    private String age;

    public User(String name, String age) {
    
        this.name = name;
        this.age = age;
    }

    public String getName() {
    
        return name;
    }

    public void setName(String name) {
    
        this.name = name;
        // 全量刷新
        notifyChange();
    }

    @Bindable
    public String getAge() {
    
        return age;
    }

    public void setAge(String age) {
    
        this.age = age;
        // single refresh
        notifyPropertyChanged(BR.age);
    }
}

编写databingding样式的xml

<?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="user" type="com.example.databingding.User" />

    </data>

    <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context=".MainActivity">

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@{user.age}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

        <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="@{user.name}" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />

        <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:text="改变参数" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintRight_toRightOf="parent" app:layout_constraintTop_toTopOf="parent" />
    </LinearLayout>
</layout>

Activity中使用ViewModel

public class MainActivity extends AppCompatActivity {
    

    @Override
    protected void onCreate(Bundle savedInstanceState) {
    
        super.onCreate(savedInstanceState);
        ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        User user = new User("张三", "60");
        mainBinding.setUser(user);
        mainBinding.button.setOnClickListener(new View.OnClickListener() {
    
            @Override
            public void onClick(View v) {
    
                user.setAge("30");
                user.setName("李思");
            }
        });
    }
}

There is another way of defining data,Binding data is the same as above

// The second way of one-way refresh
public class Bean {
    

    public ObservableField<String> id = new ObservableField<>();

    public ObservableField<String> name = new ObservableField<>();

}

双向绑定

只需要修改一下xml就可以,在@后面加上一个 = 符号

比如:

        <EditText android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@={user.name}" />

The complete example code is attached for referenceDataBinding例子代码

copyright notice
author[Someone from Yungu Li],Please bring the original link to reprint, thank you.
https://en.cdmana.com/2023/025/202301251120395496.html

Random recommended