MvvM 分为Model层,View层,ViewModel(VM)层;
Model层负责数据请求,ViewModel(vm)层则是与MVP 中的Presenter层起到的作用是一至的,绑定Model与View层,并进行数据处理等操作。View则是展示数据的部分。三个部分通过ViewModel层进行连接与绑定,使得MVVM兼得Mvp的优点。
MvvM的使用,首先需要在项目gradle中进行如下配置,开启dataBing功能。开启之后即可以在布局中进行数据绑定操作。
android{
dataBinding{
enabled true
}
}
下一步
DataBindingUtil 的使用
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android"> <!-- 将需要绑定数据的布局使用 layout包裹, 包裹完成之后系统会自动生成对应的Binding类 --> <data> <!-- data是进行数据绑定的地方,所有需要用到的类都需要在这里声明--> <variable name="bean" type="com.guangmo.stockopt.bean.BalanceBean" /><!--variable 为声明类的标签,name中填的是类的对象, 使用name直接可调用类中方法--> <variable name="user" type="com.guangmo.stockopt.bean.UserBean" /> <!--variable 为声明类多个对象以此类推--> </data> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="@{user.name}" /> <!-- 在布局中直接绑定数据--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text='@{user.name==null?"":user.name}' /> <!-- 在布局中的判断可以使用三元运算等--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@{user::myOnClick}" /> <!-- 需要先和数据一样声明点击事件所在的类--> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:onClick="@{(view)->user.myOnClick(view)}" /> <!-- 绑定点击事件的2种写法--> <!-- user中的点击事件只需要这么写 public void myOnClick(View view){ }--> </LinearLayout> </layout>
import android.databinding.DataBindingUtil; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v7.app.AppCompatActivity; import android.view.View; import com.lxy.webviewvideoplay.databinding.AaaaBinding; /** * Created by LXY on 2018/6/14. */ public class MvvmDemoActivity extends AppCompatActivity { AaaaBinding mBinding; UserBean userBean; Beans beans; @Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); mBinding=DataBindingUtil.setContentView(this,R.layout.aaaa);//绑定布局 userBean=new UserBean(); userBean.setName("2333"); mBinding.setUser(userBean);//绑定数据源,设置布局上声明的数据源 beans=new Beans(); mBinding.setBean(beans); //DataBinding 帮我们少了绑定id这个步骤,可以直接使用布局中的id名字来进行操作 mBinding.tvOne.setText("我是省了的布局一"); mBinding.tvTwo.setText("我是省了的布局二"); } public void myOnClick(View view){//点击事件 } }
下一步:自定义控件的布局中赋值
先是正常的编写自定义属性,并在自定义控件中为这个属性写上对应的set方法,dataBinding 会使用属性名称去查找对应的set方法,利用这一点即可在布局中进行自定义属性的数据等操作。
更新数据源只需要重新set数据源就可以实现快速更新。节省了大量的代码与时间。
RecyclerView 适配器绑定数据源
首先布局item_layout文件,写法和正常布局一致
<?xml version="1.0" encoding="utf-8"?> <layout> <data> <variable name="item" type="com.lxy.webviewvideoplay.Beans"/> </data> <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <TextView android:layout_width="match_parent" android:layout_height="match_parent" android:text="@{item.names}" /> </android.support.constraint.ConstraintLayout> </layout>
接下来是适配器,适配器是不是非常简洁,因为数据全在布局中进行了绑定因此适配器中几乎不需要任何的绑定操作
public class AdapterDemo extends RecyclerView.Adapter<AdapterDemo.ViewHolder> { ArrayList list; Context mContext; public AdapterDemo(Context context, ArrayList list){ mContext=context; this.list=list; } @Override public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { ItemLayoutBinding mBinding = DataBindingUtil.inflate(LayoutInflater.from(mContext), R.layout.item_layout, parent, false); ViewHolder holder = new ViewHolder(mBinding, mBinding.getRoot()); return holder; } @Override public void onBindViewHolder(ViewHolder holder, int position) { Beans beans=new Beans(); beans.setNames(position+"ge"); holder.binding.setItem(beans); holder.binding.executePendingBindings(); } @Override public int getItemCount() { return list.size(); } class ViewHolder extends RecyclerView.ViewHolder{ ItemLayoutBinding binding; public ViewHolder(ItemLayoutBinding binding,View itemView) { super(itemView); this.binding=binding; } } }
好了,关于MVVM的基础使用方法已经都介绍了,赶紧试试吧,
最后附上demo地址 https://github.com/IHoveYou/MvvmDemo希望对你们有所帮助