好了,继前三篇学习了Data Binding之后,我们可以发现它的强大之处有这么几点:
- 使用MVVM模式,让整个项目结构清晰明了
- 通过ViewModel连接View和Model,使得View与Model层解耦,分层后各司其职,维>护方便
- 易于项目的测试
- 可以根据id自动生成View的对象,再也不用findViewById了
好了,说了好处,当然也有不太好的地方,毕竟是今年刚刚推出来的,我总结出了两大缺点,我想以后的版本肯定会改进的:
下面通过一个Demo来看Data Binding在RecyclerView中的使用:
Model层
就只有一个User类,它继承自BaseObservable,并在getter方法中加入@Bindable注解,在setter方法中加入notifyPropertyChanged(),这样User中的数据更新时可以通知UI更新:
public class User extends BaseObservable{
private String userName;
private String userPassword;
private int userAge;
@Bindable
public String getUserName() {
return userName;
}
public void setUserName(String userName) {
this.userName = userName;
notifyPropertyChanged(com.sunzxyong.binding.BR.userName);
}
@Bindable
public String getUserPassword() {
return userPassword;
}
public void setUserPassword(String userPassword) {
this.userPassword = userPassword;
notifyPropertyChanged(com.sunzxyong.binding.BR.userPassword);
}
@Bindable
public int getUserAge() {
return userAge;
}
public void setUserAge(int userAge) {
this.userAge = userAge;
notifyPropertyChanged(com.sunzxyong.binding.BR.userAge);
}
public User(String userName, String userPassword, int userAge) {
this.userName = userName;
this.userPassword = userPassword;
this.userAge = userAge;
}
}
View层
主界面:
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools">
<data>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#03A9F4" />
<android.support.v7.widget.RecyclerView
android:id="@+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</layout>
recycler_item:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android">
<data>
<variable
name="user"
type="com.sunzxyong.binding.model.User"/>
</data>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#009688"
android:gravity="center"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.userName}"
android:textSize="20sp"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{user.userPassword}"
android:textSize="20sp"
android:textColor="#ffffff" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{String.valueOf(user.userAge)}"
android:textSize="20sp"
android:textColor="#ffffff" />
</LinearLayout>
</layout>
recycler_item中绑定了User。。。
ViewModel层:
设置Toolbar和RecyclerView:
我们通过得到ActivityMainBinding对象得到Toolbar控件和RecyclerView控件:
//设置Toolbar
ActivityMainBinding mainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
mainBinding.toolbar.setTitle("Android Data Binding代码实战");
mainBinding.toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(mainBinding.toolbar);
initData();
//设置RecyclerView
mainBinding.recyclerView.setLayoutManager(new StaggeredGridLayoutManager(2, StaggeredGridLayoutManager.VERTICAL));
MyRecyclerViewAdapter adapter = new MyRecyclerViewAdapter(this,users);
mainBinding.recyclerView.setAdapter(adapter);
BindingHolder:
public class BindingHolder extends RecyclerView.ViewHolder {
private RecyclerItemBinding binding;
public BindingHolder(View itemView) {
super(itemView);
}
public RecyclerItemBinding getBinding() {
return binding;
}
public void setBinding(RecyclerItemBinding binding) {
this.binding = binding;
}
}
MyRecyclerViewAdapter:
public class MyRecyclerViewAdapter extends RecyclerView.Adapter<BindingHolder> {
private Context mContext;
private List<User> users;
private List<Integer> heights;
public MyRecyclerViewAdapter(Context context,List<User> users) {
this.mContext = context;
this.users = users;
initHeight();
}
private void initHeight(){
heights = new ArrayList<>();
for (int i = 0; i < users.size(); i++) {
heights.add(200+(int)(300*Math.random()));
}
}
@Override
public BindingHolder onCreateViewHolder(ViewGroup parent, int viewType) {
RecyclerItemBinding mItemBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.recycler_item, parent, false);
BindingHolder mHolder = new BindingHolder(mItemBinding.getRoot());//得到根布局View设置给ViewHolder
mHolder.setBinding(mItemBinding);//把mItemBinding设置给ViewHolder
return mHolder;
}
@Override
public void onBindViewHolder(BindingHolder holder, int position) {
ViewGroup.LayoutParams params = holder.itemView.getLayoutParams();
params.height = heights.get(position);
holder.itemView.setLayoutParams(params);
//通过holder.getBinding()得到Binding Class
User user = users.get(position);
holder.getBinding().setVariable(com.sunzxyong.binding.BR.user,user);//动态设置数据
// holder.getBinding().setUser(user);这种方式也行,因为User继承自BaseObservable
holder.getBinding().executePendingBindings();//立即更新UI
}
@Override
public int getItemCount() {
return users.size();
}
}
效果:
image.png下载DemoDataBindingDemo2
网友评论