- 两种双向绑定方式(BaseObservable,ObservableField)
- 导入资源包
- variable、import
- 二级页面传参
- BindingAdapter
- RecyclerView传参
- 注意事项
1.两种双向绑定方式(BaseObservable,ObservableField)
BaseObservable
public class UserViewModel extends BaseObservable {
private User user;
public UserViewModel(){
this.user = new User("Jack");
}
//对象 绑定 控件
@Bindable
public String getUserName(){
return user.userName;
}
//控件 绑定 对象
public void setUserName(String userName){
if (userName != null || userName.equals(user.userName)){
user.userName = userName;
//通知更新
notifyPropertyChanged(BR.userName);
Log.d("jiang",userName);
}
}
}
<?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.databinding4.UserViewModel" />
</data>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<EditText
android:id="@+id/editText"
android:layout_width="180dp"
android:layout_height="64dp"
android:text="@={userViewModel.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
</androidx.constraintlayout.widget.ConstraintLayout>
</layout>
注意:
android:text="@={userViewModel.userName}" 有个等号
ObservableField
public class UserViewModel {
private ObservableField<User>userObservableField;
public UserViewModel(){
User user = new User("Jack");
userObservableField = new ObservableField<>();
userObservableField.set(user);
}
public String getUserName(){
return userObservableField.get().userName;
}
public void setUserName(String userName){
userObservableField.get().userName = userName;
Log.d("jiang",userObservableField.get().userName);
}
}
<EditText
android:id="@+id/editText"
android:layout_width="180dp"
android:layout_height="64dp"
android:text="@={userViewModel.userName}"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.499" />
注意:
android:text="@={userViewModel.userName}"有个等号
2.导入资源包
在模块的build.gradle文件中加入
android {
...
buildFeatures {
dataBinding true
}
}
3.variable、import
<data>
<variable
name="idol"
type="com.example.databinding.Idol" />
<import type="com.example.databinding.StarUtils"/>
<variable
name="eventHandle"
type="com.example.databinding.EventHandleListener" />
</data>
variable -> 放类对象
import -> 引入工具类
public class StarUtils {
public static String getStar(int star){
switch (star){
case 1:
return "一星";
case 2:
return "二星";
case 3:
return "三星";
case 4:
return "四星";
case 5:
return "五星";
}
return "";
}
}
public class EventHandleListener {
private Context context;
public EventHandleListener(Context context) {
this.context = context;
}
public void buttonOnClick(View view){
Toast.makeText(context,"喜欢",Toast.LENGTH_SHORT).show();
}
}
4.二级页面传参
一级页面
二级页面
注意:一级页面的【app:idol]】对应 二级页面的【idol】
5.BindingAdapter自定义加载网络图片
Adapter类
布局文件
注意:
@BindingAdapter("")中的参数 对应 app:image
BindingAdapter多参数重载
注意:当不需要所有参数的时候
设置requireAll = false
6.RecyclerView传参
RecyclerViewAdapter
public class RecyclerViewAdapter extends RecyclerView.Adapter<RecyclerViewAdapter.MyViewHolder> {
List<Idol> idols;
public RecyclerViewAdapter(List<Idol> idols) {
this.idols = idols;
}
@NonNull
@Override
public MyViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
ItemBinding itemBinding = DataBindingUtil.inflate(LayoutInflater.from(parent.getContext()),R.layout.item,parent,false);
return new MyViewHolder(itemBinding);
}
@Override
public void onBindViewHolder(@NonNull MyViewHolder holder, int position) {
Idol idol = idols.get(position);
holder.itemBinding.setIdol(idol);
}
@Override
public int getItemCount() {
return idols.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private ItemBinding itemBinding;
public MyViewHolder(ItemBinding itemBinding) {
super(itemBinding.getRoot());
this.itemBinding = itemBinding;
}
}
}
7.注意事项
(1)如果类的属性为int类型,设置String.valueOf()方法转型
(2)调用ViewModel中的方法