Android-Data Binding

(PS:文章内容为参考Android developer 翻译所得成果.)

简介

Data binding 在2015年7月发布的Android Studio v1.3.0 版本上引入,在2016年4月Android Studio v2.0.0 上正式支持。目前为止,Data Binding 已经支持双向绑定了。

Databinding 是一个实现数据和UI绑定的框架,是一个实现 MVVM 模式的工具,有了 Data Binding,在Android中也可以很方便的实现MVVM开发模式。

Data Binding 是一个support库,最低支持到Android 2.1(API Level 7+)。

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

使用

只需在build.gradle文件中添加依赖即可,文件形式如下:

android {
    compileSdkVersion 25
    buildToolsVersion "25.0.0"
    defaultConfig {
        ....
        dataBinding{
            enabled true
        }
    }
    ...
}

如上指定dataBinding enabled 为true即可。

例子

接下来,我们编写一个简单的DataBinding的例子,首先按照指导,创建与视图绑定的实体类(PS:以最常见的User为例),该类拥有三个属性,具体内容如下:

public class User {
    public static final String TAG = "User";
    private int userImage;
    private String userName;
    private String userLocation;

    public User(int userImage, String userName, String userLocation) {
        this.userImage = userImage;
        this.userName = userName;
        this.userLocation = userLocation;
    }

    public User() {

    }

    public int getUserImage() {

        return userImage;
    }

    public void setUserImage(int userImage) {
        this.userImage = userImage;
    }

    public String getUserName() {
        return userName;
    }

    public void setUserName(String userName) {
        this.userName = userName;
    }

    public String getUserLocation() {
        return userLocation;
    }

    public void setUserLocation(String userLocation) {
        this.userLocation = userLocation;
    }
}

创建与该类绑定的布局文件:

<?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="user"
            type="packageName.User" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="horizontal">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_margin="5dp"
            android:orientation="horizontal"
            android:padding="10dp">

            <ImageView
                android:layout_width="50dp"
                android:layout_height="50dp"
                android:src="@mipmap/ic_launcher"
                />

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_gravity="center_vertical"
                android:layout_marginLeft="10dp"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{user.userName}"
                    android:textAppearance="?android:attr/textAppearanceMedium"
                    android:textColor="@android:color/black"
                    tools:text="Name" />

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:text="@{`` + user.userLocation}"
                    android:textColor="@android:color/black"
                    tools:text="Location" />

            </LinearLayout>
        </LinearLayout>
    </LinearLayout>
</layout>

从上面布局文件可以看出,其需要声明一个variable抽象对象,所有布局中控件的具体内容依赖于该抽象对象的成员变量,另外需要注意根布局是layout,且没有layout_widthlayout_height属性。

修改MainActivity.javaonCreate()函数如下所示:

 @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);  mRecyclerView.setAdapter(mRecyclerViewAdapter);
        RecyclerItemBinding binding = DataBindingUtil.setContentView(this, R.layout.recycler_item);
        User user = new User(R.mipmap.ic_launcher,"Test", "User");
        binding.setUser(user);

    }

这里需要注意,XXXXXBinding.java这个类是根据你需要绑定的布局文件动态生成的,make 工程后就会出现,生成规则遵循驼峰命令法,例如说,布局文件名为main_activity.xml,则生成的类为MainActivityBinding.java,这里我的布局文件名为recycler_item.xml,所以生成的类为RecyclerItemBinding.java

参考链接

google developer
Android-Data-Binding-系列-一-详细介绍与使用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值