android databing的学习(一)

     Data binding 在2015年7月发布的android Studio v1.3.0 版本上引入,在2016年4月Android Studio v2.0.0 上正式支持。目前为止,Data Binding 已经支持双向绑定了,实在2016年的google I/O大会上发布的。现在已经很普及啦,在项目中也在慢慢使用。

    一、先简单介绍一下概念性的东西:

    Databinding 是一个实现数据和UI绑定的框架,是一个实现 MVVM 模式的工具,有了 Data Binding,在Android中也可以很方便的实现MVVM开发模式。会java web开发的会更好的理解在xml中绑定数据的模式,在web开发中也是使用@{}来实现数据的显示的。

    Data Binding 是一个support库,最低支持到Android 2.1(API Level 7+)。使用DataBing,Gradle的Android Plugin需要在1.5.0-alpha1以上。

    Data Binding 之前,我们不可避免地要编写大量的毫无营养的代码,如 findViewById()、setText(),setVisibility(),setEnabled() 或 setOnClickListener() 等,通过 Data Binding , 我们可以通过声明式布局以精简的代码来绑定应用程序逻辑和布局,这样就不用编写大量的毫无营养的代码了。

   当然,databing还是有一些缺点的比如:

1、ViewModel与View一一对应; 
2、使用起来灵活性比较低; 
3、Model属性发生变化时,ViewDatabinding采用异步更新数据,对于现实大量数据的ListView,会有一定延迟,在实践测试中发现,Databing效率较低,对于负责的界面不太适用; 
4、自动生成大量代码和属性字段:ViewDataBinding 实现类 DataBinderMapper 等。

二、databinding的使用

DataBinding的使用还是比较简单的,在build.gradle里面加入 dataBinding { enabled =true }这个配置就可以使用来。

先来大约看一些他的大约的使用:这是自己写的一个注册页面

binding= DataBindingUtil.setContentView(this,R.layout.activity_register);
registerViewModle=new RegisterViewModle();
binding.setRegisterViewModle(registerViewModle);
binding.setLoginBean(new LoginBean());

<layout>
    <data>
        <variable
            name="registerViewModle"
            type="com.sd.schools.component.login.viewmodel.RegisterViewModle"/>

        <variable
            name="loginBean"
            type="com.sd.schools.component.login.model.LoginBean"/>
    </data>

<RelativeLayout 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"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.sd.schools.component.login.view.RegisterActivity">



    <RelativeLayout

        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context="com.sd.schools.view.LoginActivity"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentStart="true">


        <LinearLayout
            android:layout_marginRight="@dimen/px50"
            android:layout_marginLeft="@dimen/px50"
            android:layout_marginTop="@dimen/px50"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:id="@+id/ln_edit"
            android:background="@color/white"
            >

            <EditText
                android:id="@+id/user_mobile"
                android:layout_width="match_parent"
                android:layout_height="@dimen/px100"
                android:layout_marginLeft="@dimen/px10"
                android:layout_marginRight="@dimen/px10"
                android:background="@null"
                android:hint="请输入手机号"
                android:text="@={loginBean.mobile}"
                android:textColor="@color/c999999"
                android:textColorHint="@color/c48c7b0" />

            <View
                android:layout_marginRight="@dimen/px10"
                android:layout_marginLeft="@dimen/px10"
                android:layout_width="match_parent"
                android:layout_height="@dimen/px2"
                android:background="@color/c48c7b0"
                />
            <LinearLayout
                android:layout_width="match_parent"
                android:orientation="horizontal"
                android:layout_height="wrap_content">
                <EditText
                    android:background="@null"
                    android:textColor="@color/c999999"
                    android:layout_marginRight="@dimen/px10"
                    android:layout_marginLeft="@dimen/px10"
                    android:textColorHint="@color/c48c7b0"
                    android:hint="填写验证码"
                    android:text="@={loginBean.code}"
                    android:layout_width="0dp"
                    android:layout_weight="1"
                    android:maxEms="11"
                    android:layout_height="@dimen/px100"
                    />
                <Button
                    android:layout_width="wrap_content"
                    android:textColor="@color/c48c7b0"
                    android:layout_gravity="center"
                    android:gravity="center"
                    android:background="@color/white"
                    android:layout_marginRight="@dimen/px10"
                    android:layout_marginLeft="@dimen/px10"
                    android:layout_height="match_parent"
                    android:text="发送验证码"
                    android:maxEms="4"
                    android:inputType="number"
                    android:onClick="@{(view)->registerViewModle.registerClick(view,loginBean)}"
                    />
            </LinearLayout>

            <View
                android:layout_marginRight="@dimen/px10"
                android:layout_marginLeft="@dimen/px10"
                android:layout_width="match_parent"
                android:layout_height="@dimen/px2"
                android:background="@color/c48c7b0"
                />
            <EditText
                android:inputType="textPassword"
                android:maxEms="12"
                android:text="@={loginBean.password}"
                android:background="@null"
                android:id="@+id/user_pass"
                android:textColor="@color/c999999"
                android:layout_marginRight="@dimen/px10"
                android:layout_marginLeft="@dimen/px10"
                android:textColorHint="@color/c48c7b0"
                android:hint="请输入账号密码"
                android:layout_width="match_parent"
                android:layout_height="@dimen/px100"
                />
        </LinearLayout>


        <Button
            android:onClick="@{(view)->registerViewModle.registersClick(loginBean)}"
            android:layout_marginTop="@dimen/px320"
            android:layout_width="@dimen/px200"
            android:layout_alignParentRight="true"
            android:layout_marginRight="@dimen/px25"
            android:layout_height="@dimen/px80"
            android:text="注册"
            android:textColor="@color/white"
            android:textSize="@dimen/px26"
            android:id="@+id/btn_login"
            android:background="@color/c48c7b0"


            />

    </RelativeLayout>





</RelativeLayout>
</layout>


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值