DataBinding

使用动力:

  1、缩减代码,findviewbyid、注解都不想用,彻底的不想声明view相关的变量

  2、装逼。。。好歹多年程序员,没用过多丢脸啊

说明:

  1、目前只是基于缩减代码使用,不涉及性能,

  2、身为CV信徒,必须基础封装,

集成:

    1、依赖:在项目的build.gradle文件里的Android节点下

        android {

            dataBinding {

            enabled =true

            }

        }

    2、xml

                (1)、进入xml布局文件,焦点设置在根节点上(左键点一下就好),alt+enter后,选择 Convert to data binding layout 然后回车

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

</data>

<TextView

android:layout_width="match_parent"

android:layout_height="match_parent">

</TextView>

</layout>

android:src="@{selected==0?@drawable/icon_home_select : @drawable/icon_home_select_un}"

                        layout:布局根节点必须是<layout> . 同时layout只能包含一个View标签. 不能直接包含<merge>

                        <data>:标签的内容即DataBinding的数据. data标签只能存在一个

                    (2)、在data节点下可以添加两种节点

<data>

<import type="com.libs.utils.dataUtil.date.DateUtil" />

<import type="android.view.View" />

<import type="android.text.TextUtils" alias="textUtlis" />

<variable name="du" type="DateUtil" />

<variable name="du2" type="com.libs.utils.dataUtil.date.DateUtil" />

<variable name="isLogin" type="Boolean" />

</data>

                                variable :变量,name是变量名,不能包含_下划线,type是变量的类型,布尔等基础类型可以直接写Boolean等,也可以填入实体类或者工具类但包名路径必须写全,

                                 import :导入,默认导入了java/lang包下的类(String/Integer),所以不写import的时候,也可以在data里直接用Boolean等,

                        (3)、include可以使用关联,merge不支持

<include

layout="@layout/layout_include"

bind:user="@{user}" />

 引用xml里注意:xmlns:bind="http://schemas.android.com/apk/res-auto" 这个要有,有时候as提示添加不出来                   

                             layout_include布局中也需要声明user变量:

<?xml version="1.0" encoding="utf-8"?>

<layout xmlns:android="http://schemas.android.com/apk/res/android">

<data>

<variable name="user" type="com.yl.databindingdemo.bean.User" />

</data>

<LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical">

</LinearLayout>

</layout>

                         (4)关联布局   @{} 格式引用

                            (5) 逻辑表达式:不需要转义的有 > ,  || , == , != ;需要转义的有: >  =  &gt;   < = &lt;    & = &amp;    " = &quot;


            3、java代码:以后再写


            4、关联Glide,加载图片( @BindingAdapter())

/** * binding 关联Glide 加载图片 * * @param imageView 载体 * @param url 路径 * @param holderDrawable 等待占位图 * @param errorDrawable 失败占位图 * <p> * app:srcUrl;xml里关联路径 * app:holder;xml里关联等待占位图 * app:error;xml里关联失败占位图 * as不提示app依赖的时候手动添加 xmlns:app="http://schemas.android.com/apk/res-auto" */@BindingAdapter({"app:srcUrl", "app:holder", "app:error"})public static void loadImage(ImageView imageView, String url, Drawable holderDrawable, Drawable errorDrawable) { Glide.with(imageView.getContext()).load(url).placeholder(holderDrawable).error(errorDrawable).into(imageView);}


<ImageView

android:layout_width="86dp"

android:layout_height="86dp"

app:srcUrl="@{detailBean.buildImgStr}"

app:holder="@{@drawable/position_icon}"

app:error="@{@drawable/position_icon}"

/>

©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容