Jetpack DataBinding(数据绑定库)笔记

官方文档地址:https://developer.android.google.cn/topic/libraries/data-binding

作用或目的:将数据直接绑定到布局里面,减少了Activity/Fragment的绑定控件操作,听说有助于防止内存泄漏以及避免发生 Null 指针异常。感觉类似于当年写JSP的时候把数据传到了页面。

具体步骤:

首先在Gradle里面集成这个依赖包。

    1.在你的项目Module里面首行加入支持:apply plugin: 'kotlin-kapt'

    2.在android的配置信息里加入绑定开关:

dataBinding {
        enabled true
    }

然后在我们的布局的根布局上按下「 ALT+回车」选择 “Convert to data binding layout”选项,就会自动产生相应的布局

变化后的

在data里引入传入的数据,可以直接申明变量,如果多出引用到这个类呢,就引入就好了


<!--直接声明-->
<data>
        <variable
            name="accountInfo"
            type="com.huishine.myjetpack.ui.login.AccountInfo" />
    </data>

<!--多处引用-->
<data>
        <import type="com.huishine.myjetpack.ui.login.AccountInfo" />
        <variable
            name="accountInfo"
            type="AccountInfo" />
    </data>

然后相应的控件就可以使用这个声明为"accountInfo"的变量了,在相应的位置使用"@{accountInfo.data}"这样的样式进行引用,default可以设置默认值

<?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>
        <import type="com.huishine.myjetpack.ui.login.AccountInfo" />
        <variable
            name="accountInfo"
            type="AccountInfo" />
    </data>

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/main"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        tools:context=".ui.login.LoginFragment">

        <TextView
            android:id="@+id/account"
            android:layout_width="150dp"
            android:layout_height="150dp"
            android:gravity="center"
            android:text="@{accountInfo.account,default=admin}"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

    </androidx.constraintlayout.widget.ConstraintLayout>
</layout>

如果有同类名的类,则可以通过别名来引用

<data>

        <import type="com.huishine.myjetpack.ui.login.AccountInfo" />

        <import
            alias="AccountInfo2"
            type="com.huishine.myjetpack.ui.main.AccountInfo" />

        <variable
            name="accountInfo"
            type="AccountInfo" />

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

然后在创建布局的时候进行配置,系统为自动生成一个布局名首写大字母的驼峰类名,如下绑定就可以了

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = DataBindingUtil.inflate<LoginFragmentBinding>(
            inflater,
            R.layout.login_fragment,
            container,
            false
        )
        return binding.root
    }

然后赋值就像以下方式就可以了

override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        val binding = DataBindingUtil.inflate<LoginFragmentBinding>(
            inflater,
            R.layout.login_fragment,
            container,
            false
        ).apply {
            accountInfo=AccountInfo("0000", "1111")
        }
        return binding.root
    }

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值