android data binding jetpack VIII BindingConversion

android data binding jetpack VII @BindingAdapter

android data binding jetpack V 实现recyclerview 绑定

android data binding jetpack IV 绑定一个方法另一种写法和参数传递

android data binding jetpack III 绑定一个方法

android data binding jetpack II 动态数据更新

android data binding jetpack I 环境配置 model-view 简单绑定

@BindingConversion

绑定转换

意思是view某个属性需要一个值,但数据所提供的值跟这个需求有区别。

比如:background 需要drawable,但用户提供的是color int值或者是"#FFFFFF"这样的字符窜,那就不能满足了。需要转换一下。

@BindingConversion是用来满足这样的需求的,把“#FFFFFF”转成drawable

用法:

1.在任何一个类里面编写方法,使用@BindingConversion注解。这个与之前的bindingadapter一个用法。很难理解为什么这么做,之后再去看源码分析。

2.主法名随便起,叫啥都行。比如 intToDrawable

3.方法必须为公共静态(public static)方法,且有且只能有1个参数

4.重点:方法作用在什么地方的决定因素是:参数类型和返回值类型。两个一致系统自动匹配。

5.转换仅仅发生在setter级别,因此它是不允许以下混合类型:

<View
android:background="@{isError ? @drawable/error : @color/white}"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> 例子:
    /**
* 将字符串颜色值转化为ColorDrawable
* 随例放哪个类里都行。随便叫啥都行。
* @param colorString 如:#ff0000
* @return
*/
@BindingConversion
public static ColorDrawable convertColorStringToDrawable(String colorString) {
int color = Color.parseColor(colorString);
return new ColorDrawable(color);
}
  我们在提供数据的类里定义如下:
   private String color;
public String getColor() {
return color;
}
  在V里绑定时使用他。     
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:background="@{student.color}"
android:gravity="center"
android:text="@{student.name}"
android:textSize="18sp" />
效果:

有文章说慎重使用text转int。

使用注意
慎用convertStringToInt:

@BindingConversion
public static int convertStringToInt(String value)
{
int ret;
try {
ret = Integer.parseInt(value);
}
catch (NumberFormatException e)
{
ret = 0;
}
return ret;
}

如果使用databinding的android:text赋值是string类型,会被转换为int类型:

<variable
name="str"
type="String"/>
……………
<TextView
android:id="@+id/id_chute_group_one_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@{str}" />

this.idTxt.setText(com.biyou.databinding.Converters.convertStringToInt(str));

运行的时候会报错:

android.content.res.Resources$NotFoundException: String resource ID #0x0
---------------------
作者:逆风Lee
来源:CSDN
原文:https://blog.csdn.net/lixpjita39/article/details/79049872
版权声明:本文为博主原创文章,转载请附上博文链接!

这个错误的原因是:正好TextView 有setText(int)方法,int 是RES资源。很好理解。

重点 重点 重点:方法作用在什么地方的决定因素是:参数类型和返回值类型。两个一致系统自动匹配。

总结前面两个知识点。

当数据和V的属性不匹配的时候可以有两个做法:

1.定义一个转换方法,使用bindingAdapter 起一个新的属性,在方法里对V进行操作。

2.使用BindingConversion转换成对应的资源类型。也就是本文。

 

最新文章

  1. Eclipse 开发 jsp
  2. BlacJack游戏
  3. C#中 MD5和SHA1加密代码
  4. java按值传递相关理解
  5. frequentism-and-bayesianism-chs-iii
  6. C语言输出单个汉字字符
  7. 基于sqlite的Qt 数据库封装
  8. JavaScript检测实例属性, 原型属性
  9. MD5算法步骤详解
  10. VS 制作安装包小窥
  11. 当fixed元素相互嵌套时chrome下父元素会影响子元素的层叠关系
  12. —页面布局实例———win7自己的小算盘
  13. [Awson原创]洪水(flood)
  14. 12.Git分支-推送(push)、跟踪分支、拉取(pull)、删除远程分支
  15. Java内省机制
  16. PHP Warning: mysqli_connect(): The server requested authentication method unknown to the client [caching_sha2_password] in /usr/local/php/CreateDB.php on line 5
  17. [LeetCode&amp;Python] Problem 720. Longest Word in Dictionary
  18. 关于Django部分
  19. P4556 [Vani有约会]雨天的尾巴
  20. jQuery stop()浅析

热门文章

  1. 利用axis调用webservice接口
  2. 使用jMeter构造大量并发HTTP请求进行微服务性能测试
  3. 3.第一个MyBatis程序_进化
  4. 【2017-06-02】Linq高级查询,实现分页组合查询。
  5. python学习笔记:安装boost python库以及使用boost.python库封装
  6. sql 分页查询 (每次6行 )
  7. 如何让iframe框架和主页面共用一个滚动条(也称为:iframe高度自适应问题)
  8. Ubuntu系统---WeChat安装
  9. [暂停维护]基于8211lib库对s57电子海图的解析和存储
  10. nginx+tomcat遇到的https重定向到http问题