JetPack - - - DataBinding

本文详细介绍了Android的JetPack组件DataBinding的使用,包括其作用、简单应用、在DataBinding中引用函数、使用<include>及BindingAdapter实现二级页面绑定、双向绑定的BaseObservable和ObservableField方法,以及在RecyclerView和DataBinding+ViewModel+LiveData中的应用,通过实例演示各个功能的实现效果。
摘要由CSDN通过智能技术生成

DataBinding的作用

让布局文件承担了部分原本属于页面的工作,使页面与布局耦合度进一步降低。

DataBinding的应用

1.简单应用

首先在gradle中配置

android {
    ...
    buildFeatures {
         dataBinding = true
    }
}

我们先写一个要在Xml文件引用的数据类

public class Idol {
    public String name;

    public String star;

    public Idol(String pName, String pStar) {
        name = pName;
        star = pStar;
    }
}

然后在Xml文件里引用我们写好的数据类,并引用在控件上。

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <!--引用写好的数据类-->
        <variable
            name="Idol"
            type="com.example.jetpacktest.Idol" />
         <!--drawable的引用-->
        <variable
            name="LocalImage"
            type="int" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DataBindingTest.DataBindingActivity">


        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:defaultImageResource="@{LocalImage}"
            tools:srcCompat="@tools:sample/avatars" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="TextView"
            android:textSize="24sp"
            android:text="@{Idol.name}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="@+id/guideline3"
            app:layout_constraintVertical_bias="0.167" />

        <TextView
            android:id="@+id/textView4"
            android:layout_width="72dp"
            android:layout_height="20dp"
            tools:text="TextView"
            android:textSize="18sp"
            android:text="@{Idol.star}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/name"
            app:layout_constraintVertical_bias="0.204" />

        <Button
            android:id="@+id/button5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="喜欢"
            android:text="喜欢"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintHorizontal_bias="0.498"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toBottomOf="@+id/textView4"
            app:layout_constraintVertical_bias="0.643" />
   
    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

然后在activity里绑定DataBinding并给Xml文件里配置的Data属性赋值

public class DataBindingActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       //绑定布局,得到DataBinding对象
       ActivityDataBindBinding mActivityDataBindBinding= DataBindingUtil.setContentView(this,R.layout.activity_data_bind);
        Idol mIdol = new Idol("xiaohua", "4星");
        //赋值
        mActivityDataBindBinding.setIdol(mIdol);
        mActivityDataBindBinding.setLocalImage(R.drawable.ic_launcher_background);
    }
}

结果演示:

                

2.在DataBinding中引用函数

首先还是先写一个数据类

public class Idol {
    public String name;

    public int star;

    public Idol(String pName, int pStar) {
        name = pName;
        star = pStar;
    }
}
public class StarUtils {

    public static String setStar(int  star){
        switch (star){
            case 1:
                return "1星";
            case 2:
                return "2星";
            case 3:
                return "3星";
            case 4:
                return "4星";
            case 5:
                return "5星";
            default:
                return null ;
        }
    }
}

这是一个工具类。

public class EventHandleListener {
    private Context mContext;

    public EventHandleListener(Context pContext) {
        mContext = pContext;
    }

    public void buttonOnclick(View pView){
        Toast.makeText(mContext,"老二次元",Toast.LENGTH_SHORT).show();
    }
}

定义一个Button点击事件监听的类。

<layout xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:android="http://schemas.android.com/apk/res/android">

    <data>
        <!--引用写好的数据类-->
        <variable
            name="Idol"
            type="com.example.jetpacktest.Idol" />
        
         <!--Button点击事件函数-->
        <variable
            name="eventHandle"
            type="com.example.jetpacktest.EventHandleListener" />
       
        <!--drawable的引用-->
        <variable
            name="LocalImage"
            type="int" />
        <!--引入后,可以调用他的函数-->
        <import type="com.example.jetpacktest.DataBindingTest.StarUtils"/>
       
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".DataBindingTest.DataBindingActivity">


        <androidx.constraintlayout.widget.Guideline
            android:id="@+id/guideline3"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:orientation="horizontal"
            app:layout_constraintGuide_percent="0.5" />

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="200dp"
            android:layout_height="200dp"
            app:layout_constraintBottom_toTopOf="@+id/guideline3"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent"
            app:defaultImageResource="@{LocalImage}"
            tools:srcCompat="@tools:sample/avatars" />

        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            tools:text="TextView"
            android:textSize="24sp"
            android:text="@{Idol.name}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:l
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值