Google官方支持的MVVM架构框架Data Binding使用入门

最新最新推出的MVVM架构是将MVP中的Presenter换成了View Model,也就是Model +View+ViewModel的模式。这种架构方式可以很便捷的进行数据的交互,当数据发生变化时通过ViewModel可以及时的将变化反应到View中去。

本文参考自:

http://www.imooc.com/learn/719

http://www.imooc.com/learn/720

DataBinding的引入:

在需要应用DataBinding的Module的gradle文件中添加:

dataBinding{
        enabled = true
    }

即可。

DataBinding的使用:

在原本的layout布局文件中将最外层的布局标签替换为:

< layout >
	//这里边是原本的布局文件
< / layout>

//例如
<layout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:tools="http://schemas.android.com/tools">

    <data>
        <variable
            name="item"
            type="com.wei.rxjavademo.Item"/>

        <variable
            name="presenter"
            type="com.wei.rxjavademo.MainActivity.Presenter"/>
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        android:paddingBottom="@dimen/activity_vertical_margin"
        android:paddingLeft="@dimen/activity_horizontal_margin"
        android:paddingRight="@dimen/activity_horizontal_margin"
        android:paddingTop="@dimen/activity_vertical_margin"
        tools:context="com.wei.rxjavademo.MainActivity">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@{item.content}"/>

        <Button
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="订阅"/>

        <Button
            android:onClick="@{() -> presenter.onClickListenerBinding(item)}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消订阅"/>

        <EditText
            android:onTextChanged="@{presenter.onTextChanged}"
            android:layout_width="match_parent"
            android:layout_height="50dp"/>
    </LinearLayout>
</layout>


同时在布局文件中加入data标签,用于指定要绑定的数据类:

<data>
	<variable
		name=""
		type=""/>
	
</data>

//例如

<data>
        <variable
            name="item"
            type="com.wei.rxjavademo.Item"/>
        <!--name可以随意根据需要命名,type为该类型的全类名-->

        <variable
            name="presenter"
            type="com.wei.rxjavademo.MainActivity.Presenter"/>
</data>

修改完布局文件之后就可以在Java代码中使用DataBinding了。

mItem = new Item(12, "hello");
        //AS会为每一个layout标签自动生成一个Binding类,用于绑定视图
        activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);
        
        //向布局文件中的variable设置变量
        activityMainBinding.setItem(mItem);
        activityMainBinding.setPresenter(new Presenter());

Item只是自定义的一个JavaBean,里边只有Id和Content两个属性及其get和set方法

以上就是DataBinding的数据的绑定,接下来还有方法的绑定。

方法的绑定有两种情况:

  • 方法引用绑定:主要是绑定一些已经原有的方法事件,例如onClick,onTextChanged这些方法

  • 监听器绑定:主要是绑定一些自定义的方法事件,可以支持传入数据类型。

方法引用绑定:

自定义一个Presenter类,并在其中实现onClick,onTextChanged等需要的方法,然后在布局文件中直接声明。

在Java中实现onClick方法

public class Presenter {
	public void onClick(View view){
            Toast.makeText(MainActivity.this, "onClick点击", Toast.LENGTH_SHORT).show();
        }
}

在布局文件中直接声明方法的引用:

<Button
   android:onClick="@{presenter.onClick}"
   android:layout_width="match_parent"
   android:layout_height="wrap_content"
   android:text="订阅"/>
<!--在onClick方法中直接声明引用的方法为Presenter中的onClick方法-->

监听器绑定:

也是要先在Presenter中定义需要的方法,并定义好参数:

public class Presenter {
	//在这个方法中可以添加需要的参数
	public void onClickListenerBinding(Item item){
            Toast.makeText(MainActivity.this, item.getContent(), Toast.LENGTH_SHORT).show();
        }
}

只是在布局文件中引用的时候有所不同:

<Button
            android:onClick="@{() -> presenter.onClickListenerBinding(item)}"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:text="取消订阅"/>
<!--在布局文件中的使用方式为Lambda表达式-->

这样就完成了监听器的绑定。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值