本系列目录

什么是BindingAdapter

BindingAdapter用来设置布局中View的自定义属性,当使用该属性时,可以自定义其行为。

下面是一个简单的例子:

@BindingAdapter("android:bufferType")
public static void setBufferType(TextView view, TextView.BufferType bufferType) {
view.setText(view.getText(), bufferType);
}

当一个方法加上@BindingAdapter注解后,就定义了一个BindingAdapter,注意方法的第一个参数是需要绑定到的View,第二个参数是绑定的属性值。

当定义完成后,此时我们就可以在布局的View中使用该属性,举例如下:

<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:bufferType="normal"
/>

当TextView中加入了android:bufferType="normal"后,setBufferType()方法就会被调用。

当自定义其它一些属性时,也遵循一样的规则。

自定义图片加载的BindingAdapter

由于BindingAdapter的特性,我们就可以为ImageView自定义一个BindingAdapter,从而大幅简化图片加载的过程。

第一步,我们先新建一个ImageBindingAdapter的类,图片相关的BindingAdapter可以都定义在这个类里面:

public class ImageBindingAdapter {

    @BindingAdapter("imageUrl")
public static void bindImageUrl(ImageView view, String imageUrl){
RequestOptions options =
new RequestOptions()
.centerCrop()
.dontAnimate(); Glide.with(view)
.load(imageUrl)
.apply(options)
.into(view);
} }

定义好后,我们就可以直接在布局中使用这个属性了:

<ImageView
android:layout_width="180dp"
android:layout_height="180dp"
app:imageUrl="@{user.photo}"
/>

仅仅简单的一行代码,就可以进行网络图片的加载了,是不是感觉这个世界简单了很多?

除了这种单个参数的BindingAdapter,它也支持多个参数,这也是BindingAdapter强大的地方。

总结

使用BindingAdapter可以大大简化一些重复代码,本文主要介绍了加载图片上的使用,你可以举一反三,用在更多的场景中使用,比如加载列表的数据等,这样做以后也可以使您的代码更加清晰高效。

如有更多疑问,请参考我的其它Android相关博客:我的博客地址

Android开发教程 - 使用Data Binding(七)使用BindingAdapter简化图片加载的更多相关文章

  1. Android开发教程 - 使用Data Binding(八)使用自定义Interface

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  2. Android开发教程 - 使用Data Binding(六)RecyclerView Adapter中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  3. Android开发教程 - 使用Data Binding(一) 介绍

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  4. Android开发教程 - 使用Data Binding Android Studio不能正常生成相关类/方法的解决办法

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  5. Android开发教程 - 使用Data Binding(五)数据绑定

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  6. Android开发教程 - 使用Data Binding(四)在Fragment中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  7. Android开发教程 - 使用Data Binding(三)在Activity中的使用

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  8. Android开发教程 - 使用Data Binding(二)集成与配置

    本系列目录 使用Data Binding(一)介绍 使用Data Binding(二)集成与配置 使用Data Binding(三)在Activity中的使用 使用Data Binding(四)在Fr ...

  9. Android开发利器之Data Binding Compiler V2 —— 搭建Android MVVM完全体的基础

    原创声明: 该文章为原创文章,未经博主同意严禁转载. 前言: Android常用的架构有:MVC.MVP.MVVM,而MVVM是唯一一个官方提供支持组件的架构,我们可以通过Android lifecy ...

随机推荐

  1. Gazebo: Could not find parameter robot_description on parameter server

    robot_state_publisher looks for the parameter "robot_description" by default. The robot_st ...

  2. DNA计算机及DNA存储

    傅里叶变换到量子水平,可编程元素到原子分子核能,都可以极大的改变有机体(高级有机体都是有寿命的,例如人类),如果可以出现机械体,核能提供能量:并结合类似高级生物大脑的有机体大脑,不断学习进化,甚至优化 ...

  3. Redis (非关系型数据库) 数据类型 之 list列表类型

    Redis列表是简单的字符串列表,按照插入顺序排序.你可以添加一个元素到列表的头部(左边)或者尾部(右边) list即可以作为“栈”也可以作为"队列". 操作: >lpush ...

  4. 斐波那契数列(NOIP1997)

    题目链接:斐波那契数列 这题是数论的一个基本应用,还是很水,因为数据范围太水了,只有48,这也太小了.不过也有可能是当时的电脑速度跑得比较慢的原因.但是这个算法应该还是这个算法.主要思路就是递推求斐波 ...

  5. 爬虫模块之解决IO

    一 asyncio模块 asyncio模块:主要是帮我们检测IO(只能是网路IO). @asyncio.coroutine:装饰器 tasks:任务列表 get_event_loop:起任务 run_ ...

  6. mysql之索引查询2

    一 索引的创建 索引减慢了 写的操作,优化了读取的时间 index:普通索引,加速了查找的时间. fulltext:全文索引,可以选用占用空间非常大的文本信息的字段作为索引的字段.使用fulltext ...

  7. 2018.11.05 bzoj3124: [Sdoi2013]直径(树形dp)

    传送门 一道sbsbsb树形dpdpdp 第一问直接求树的直径. 考虑第二问问的边肯定在同一条直径上均是连续的. 因此我们将直径记下来. 然后对于直径上的每一个点,dpdpdp出以这个点为根的子树中不 ...

  8. jrebel热部署

    一,JRebel 插件 获取与安装 1,JRebel 官网下载地址https://zeroturnaround.com/software/jrebel/download/#!/free-trial P ...

  9. 执行sh脚本文件下载Github上的代码(雷霄骅的ffmpeg示例代码)

       今天想重新学习下ffmpeg,于是又来到了雷晓骅的博客,先下载了他的所有代码,这里记录一下在Windows上使用sh脚本下载GitHub上代码的过程. CygWin(最后并没有用到)    可以 ...

  10. 第9章 符合Python风格的对象

    #<流畅的Python>读书笔记 # 第9章 符合Python风格的对象 # 本章包含以下话题: # 支持用于生成对象其他表示形式的内置函数(如repr().bytes(),等等) # 使 ...