1.Andriod — JetPack (一):初识 JetPack
2.Andriod — JetPack (二):LifeCycle 的诞生
3.Andriod — JetPack (三):ViewModel 的诞生
4.Andriod — JetPack (四):BaseObservable 与 ObservableField 双向绑定
5.Andriod — JetPack (五):DataBinding + LiveData +ViewModel 简单实例
6.Andriod — JetPack (六):Room 增删改查
7.Andriod — JetPack (七):Room + ViewModel + LiveData 增删改查实例
一、什么叫单向绑定与双向绑定单向绑定:对象变化的时候 TextView 自动更新
双向绑定:对象变化的时候 EditView 自动更新。EditText 变化的时候对象的值也发生变化。
build.gradle
android {
compileSdkVersion 29
buildToolsVersion "30.0.3"
dataBinding {
enabled = true
}
}
MainActivity.java
import androidx.appcompat.app.AppCompatActivity;
import androidx.databinding.DataBindingUtil;
import androidx.databinding.ViewDataBinding;
import android.os.Bundle;
import com.example.baseobservable.databinding.ActivityMainBinding;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
activityMainBinding.setUserViewModel(new UserViewModel());
}
}
activity_main.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="userViewModel"
type="com.example.baseobservable.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@={userViewModel.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
User.java
package com.example.baseobservable;
public class User {
public String userName;
public User(String userName) {
this.userName = userName;
}
}
UserViewModel.java
package com.example.baseobservable;
import android.util.Log;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
// 并不是真正的 ViewModel
public class UserViewModel extends BaseObservable {
private User user;
public UserViewModel() {
this.user = new User("Jack");
}
/*
* 用 Bindable 进行绑定
* 只要对象发生变化 EditText 就会发生变化
*/
@Bindable
public String getUserName() {
return user.userName;
}
// EditText 中的内容发生变化就会调用 setUserName
public void setUserName(String userName) {
if(userName != null && !userName.equals(user.userName)) {
user.userName = userName;
Log.d("liang", userName);
notifyPropertyChanged(BR.userName); // 通知属性发生变化 , build 之后自动生成
}
}
}
结果:
需要修改的文件:
UserViewModel.java
package com.example.baseobservable;
import android.util.Log;
import androidx.databinding.BaseObservable;
import androidx.databinding.Bindable;
import androidx.databinding.ObservableField;
// 并不是真正的 ViewModel
public class UserViewModel {
private ObservableField<User> userObservableField;
public UserViewModel() {
User user = new User("Jack");
userObservableField = new ObservableField<>();
userObservableField.set(user); // userObservableField 与 user 关联起来
}
public String getUserName() {
return userObservableField.get().userName;
}
public void setUserName(String userName) {
Log.d("liang", userName);
userObservableField.get().userName = userName;
}
}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)