Android - DataBinding

1. 说明

写一个简单的DataBinding,主要涉及,图片、文字、按钮点击以及静态函数调用
界面:
在这里插入图片描述

2. 创建工程

activity_main.xml中添加几个基础控件:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            tools:src="@tools:sample/avatars"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textSize="26sp"
            tools:text="书名" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="作者" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="评分:五星" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:text="想要" />

    </LinearLayout>

这里涉及到数的 书名、作者、图片、评价,创建一个书的类:
Book.java:

public class Book {
    public String bookName;
    public String bookAuthor;
    public int star;
    public int bookImageRes;


    public Book(String bookName, String bookAuthor, int star, int bookImageRes) {
        this.bookName = bookName;
        this.bookAuthor = bookAuthor;
        this.star = star;
        this.bookImageRes = bookImageRes;
    }
}

然后,把数据 Book 与 activity_main 进行绑定。

3. 添加绑定

首先要在 build.gradle 中添加:

buildFeatures{
    dataBinding true
}

如图:
在这里插入图片描述
修改 build.gradle 并同步完成后,在activity_main.xml第一行,使用快捷键 alt + enter,选择 Convert to data binding layout,然后activity_main.xml中自动增加了datay元素,在data里面增加Book类的绑定

	<data>
        <variable
            name="book"
            type="com.example.databinding.Book" />
    </data>

给页面中相应元素绑定到Book类的成员变量,此时,activity_main.xml代码变成如下这样:

<?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="book"
            type="com.example.databinding.Book" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            tools:src="@tools:sample/avatars"
            app:imageResource="@{book.bookImageRes}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textSize="26sp"
            tools:text="书名"
            android:text="@{book.bookName}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="作者"
            android:text="@{book.bookAuthor + ` 著`}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="评分:五星" />

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:text="想要"/>

    </LinearLayout>
</layout>

MainActivity.java中继续绑定:

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        // 把setContentView改成下面这种写法
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Book book = new Book("重构:改善既有代码的设计", "Martin Fowler", 5, R.drawable.refactoring);
        activityMainBinding.setBook(book);
    }
}

运行程序,此时能看到界面中的图片、书名、作者能正常显示出来了。
但是,评分和按钮还未实现。

4. 绑定静态函数

Book类中的评分是使用数字 1 ~ 5 ,但是界面显示的是“ 一星、二星… ”这样的,需要写一个函数转换一下,我们写一个静态函数来转换,添加一个类 BookStarUtils.java

public class BookStarUtils {

    public static String getStar(int star) {
        switch (star) {
            case 1:
                return "一星";
            case 2:
                return "二星";
            case 3:
                return "三星";
            case 4:
                return "四星";
            case 5:
                return "五星";
        }

        return "";
    }
}

把这个静态函数绑定到界面的元素中,要在 activity_main.xml 的data下使用import导入此静态类:

	<data>
        <import type="com.example.databinding.BookStarUtils" />
    </data>

然后,绑定到评分的TextView中
在这里插入图片描述
此时 activity_main.xml 的代码如下:

<?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="book"
            type="com.example.databinding.Book" />
        <import type="com.example.databinding.BookStarUtils" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            tools:src="@tools:sample/avatars"
            app:imageResource="@{book.bookImageRes}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textSize="26sp"
            tools:text="书名"
            android:text="@{book.bookName}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="作者"
            android:text="@{book.bookAuthor + ` 著`}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="评分:五星"
            android:text="@{BookStarUtils.getStar(book.star)}"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:text="想要"/>

    </LinearLayout>
</layout>

5. 绑定Button点击

创建一个 ButtonHandleListener.java类处理Button点击事件

public class ButtonHandleListener {
    private Context context;

    public ButtonHandleListener(Context context) {
        this.context = context;
    }

    public void buttonOnClick(View view){
        Toast.makeText(context, "想要", Toast.LENGTH_SHORT).show();
    }
}

先在activity_main.xml 的data下引入此类:

	<data>
        <variable
            name="buttonHandleListener"
            type="com.example.databinding.ButtonHandleListener" />
    </data>

然后绑定到Button中
在这里插入图片描述
activity_main.xml 的完整代码如下:

<?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="book"
            type="com.example.databinding.Book" />
        <variable
            name="buttonHandleListener"
            type="com.example.databinding.ButtonHandleListener" />
        <import type="com.example.databinding.BookStarUtils" />
    </data>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical"
        tools:context=".MainActivity">

        <ImageView
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            android:scaleType="fitCenter"
            tools:src="@tools:sample/avatars"
            app:imageResource="@{book.bookImageRes}" />

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:textSize="26sp"
            tools:text="书名"
            android:text="@{book.bookName}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="作者"
            android:text="@{book.bookAuthor + ` 著`}"/>

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            tools:text="评分:五星"
            android:text="@{BookStarUtils.getStar(book.star)}"/>

        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:layout_marginTop="10dp"
            android:layout_marginBottom="20dp"
            android:text="想要"
            android:onClick="@{buttonHandleListener.buttonOnClick}"/>

    </LinearLayout>
</layout>

还需要在 MainActivity.java中把ButtonHandleListener类绑定到ActivityMainBinding
在这里插入图片描述

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        //setContentView(R.layout.activity_main);
        ActivityMainBinding activityMainBinding = DataBindingUtil.setContentView(this, R.layout.activity_main);

        Book book = new Book("重构:改善既有代码的设计", "Martin Fowler", 5, R.drawable.refactoring);
        activityMainBinding.setBook(book);
        activityMainBinding.setButtonHandleListener(new ButtonHandleListener(this));
    }
}

代码全部完成,运行效果如第1节展示效果。



6. 示例代码

https://gitee.com/jie-xio/android_samples/tree/master/DataBinding



7. 其他参考资料

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值