DataBinding的使用一

本文详细介绍了Android DataBinding的使用,包括环境配置、布局转换、数据绑定以及DataBinding文件的生成方式。通过实例展示了如何在布局中进行数据绑定,并在MainActivity中直接修改UI内容。同时,文章还探讨了DataBinding文件的不同生成方法和在不同场景下的应用,如在RecyclerView.Adapter中的使用。最后,提供了BR类在数据绑定中的作用和参考链接。

一、前言

DataBindingJetPack中用来进行双向绑定的库,可以使数据和UI进行解耦合。这里对其进行使用进行简单的记录

二、环境配置

在以前的旧版上使用DataBinding还需要添加插件和依赖库,不过目前来说无需如此复杂,只需要在build.gradle中进行简单的配置即可。这里有两种配置方式,如下:

方式一:

android {
        ...
        dataBinding {
            enabled = true
        }
    }
    

方式二:

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

三、将现有布局转换为databinding布局

如果我们要使用databinding的话,布局需要使用以下格式:

<layout xmlns:android="http://schemas.android.com/apk/res/android"
            xmlns:app="http://schemas.android.com/apk/res-auto">
        <data>
            <variable
                name="viewmodel"
                type="com.myapp.data.ViewModel" />
        </data>
        <ConstraintLayout... /> <!-- UI layout's root element -->
    </layout>
    

可以看到相较于以前的布局多了<layout></layout>标签。里面的<data></data>标签如果在实际中用不到赋值操作的话可以不写。如果我们手动改成这种布局还是稍微麻烦点的。官方提供了一种简单的方式。

首先需要使用上述方式开启databinding功能。然后在布局的中使用快捷键(MAC使用Alt+Enter),该快捷键在第6行之前的任意一处空白处即可调出
在这里插入图片描述

选择后进行确定即可变成以下代码

<?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>

    </data>

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

        <Button
            android:id="@+id/show_dialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="弹出一个对话框"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/update_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            app:layout_constraintTop_toBottomOf="@+id/show_dialog"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:text="value" />

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

四、数据绑定

将上述代码进行修改以便进行数据绑定

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="change"
            type="String" />
    </data>

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

        <Button
            android:id="@+id/show_dialog"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="弹出一个对话框"
            app:layout_constraintBottom_toBottomOf="parent"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <androidx.appcompat.widget.AppCompatTextView
            android:id="@+id/update_content"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="20dp"
            android:text="@{change}"
            app:layout_constraintTop_toBottomOf="@+id/show_dialog"
            app:layout_constraintStart_toStartOf="parent"
            app:layout_constraintEnd_toEndOf="parent"
            tools:text="value" />

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

MainActivity.kt

class MainActivity : AppCompatActivity() {
    private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(binding.root)
        binding.showDialog.setOnClickListener {
            LoginDialog().show(supportFragmentManager,"")
        }
        binding.change = "啦啦啦"
    }
}

可以看到布局生成了一个ActivityMainBinding类。通过修改该类的change就可以直接修改UI内容。

五、DataBinding文件

JetPack中有视图绑定和数据绑定,视图绑定省去了findViewById()的操作,数据绑定可以进行数据直接赋值。DataBinding文件有以下几种生成方式

第一种方式:

private val binding: ActivityMainBinding by lazy {
        ActivityMainBinding.inflate(layoutInflater)
    }

第二种方式:

val binding: ActivityMainBinding = DataBindingUtil.setContentView(
                this, R.layout.activity_main)

第三种方式:
如果是在自定义View中,则需要以下方式

constructor(context: Context) : super(context){
        initLayout(context)
    }
    /**
     * 初始化
     */
    private fun initLayout(ctx: Context){
        val layoutInflater = LayoutInflater.from(ctx)
        binding = ViewVirusResultBinding.inflate(layoutInflater,this,true)
    }

如果是在FragmentListView或者RecycleView适配器中使用数据绑定项。可以使用以下方式:

    val listItemBinding = ListItemBinding.inflate(layoutInflater, viewGroup, false)
    // or
    val listItemBinding = DataBindingUtil.inflate(layoutInflater, R.layout.list_item, viewGroup, false)

有时候我们不知道要绑定的具体类型。这时候可以使用以下方式进行绑定

    val viewRoot = LayoutInflater.from(this).inflate(layoutId, parent, attachToParent)
    val binding: ViewDataBinding? = DataBindingUtil.bind(viewRoot)

如果布局是使用其他机制扩充的,可单独绑定,如下所示:

    val binding: MyLayoutBinding = MyLayoutBinding.bind(viewRoot)

什么时候会用到这种方式呢,比如布局里面使用了<include><ViewStub>等标签。或者需要动态添加布局。可以使用这种方式

动态变量:
有时,系统并不知道特定的绑定类。例如,针对任意布局运行的 RecyclerView.Adapter 不知道特定绑定类。在调用 onBindViewHolder() 方法时,仍必须指定绑定值。

在以下示例中,RecyclerView 绑定到的所有布局都有 item 变量。BindingHolder 对象具有一个 getBinding() 方法,这个方法返回 ViewDataBinding 基类。

    override fun onBindViewHolder(holder: BindingHolder, position: Int) {
        item: T = items.get(position)
        holder.binding.setVariable(BR.item, item);
        holder.binding.executePendingBindings();
    }

注意:数据绑定库在模块包中生成一个名为 BR 的类,其中包含用于数据绑定的资源的 ID。在上例中,该库自动生成 BR.item 变量。

针对该解释可以参考https://blog.csdn.net/cunchi4221/article/details/107478770
https://blog.csdn.net/mountain_eyes/article/details/80627037

六、参考链接

  1. 使用入门 | Android 开发者 | Android Developers
工业和信息化部关于印发信息通信建设工程预算定额、工程费用定额及工程概预算编制规程的通知 发布时间:2017-02-10 来源:通信司 工信部通信[2016]451号 各省、自治区、直辖市通信管理局,中国电信集团公司、中国移动通信集团公司、中国联合网络通信集团有限公司、中国铁塔股份有限公司,相关单位: 为适应通信建设行业发展需要,合理有效控制通信建设工程投资,规范通信建设工程计价行为,根据国家法律法规及有关规定,我部对《通信建设工程概算、预算编制办法》及相关定额(2008年版)进行修订,形成了《信息通信建设工程预算定额》(共五册:第册通信电源设备安装工程、第二册有线通信设备安装工程、第三册无线通信设备安装工程、第四册通信线路工程、第五册通信管道工程)、《信息通信建设工程费用定额》及《信息通信建设工程概预算编制规程》,现予以发布,自2017年5月1日起施行。工业和信息化部《关于发布<通信建设工程概算、预算编制办法>及相关定额的通知》(工信部规〔2008〕75号)同时废止。 工业和信息化部 2016年12月30日 抄送:工业和信息化部通信工程定额质监中心 (《信息通信建设工程预算定额》、《信息通信建设工程费用定额》及《信息通信建设工程概预算编制规程》由工业和信息化部通信工程定额质监中心组织出版、发行。)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值