随笔 - 262  文章 - 0  评论 - 5  阅读 - 20万

android data binding jetpack VIII BindingConversion

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转换成对应的资源类型。也就是本文。

 

 

 
posted on   wp7ers  阅读(281)  评论(1编辑  收藏  举报
编辑推荐:
· [.NET]调用本地 Deepseek 模型
· 一个费力不讨好的项目,让我损失了近一半的绩效!
· .NET Core 托管堆内存泄露/CPU异常的常见思路
· PostgreSQL 和 SQL Server 在统计信息维护中的关键差异
· C++代码改造为UTF-8编码问题的总结
阅读排行:
· 【.NET】调用本地 Deepseek 模型
· CSnakes vs Python.NET:高效嵌入与灵活互通的跨语言方案对比
· DeepSeek “源神”启动!「GitHub 热点速览」
· 我与微信审核的“相爱相杀”看个人小程序副业
· Plotly.NET 一个为 .NET 打造的强大开源交互式图表库
< 2025年2月 >
26 27 28 29 30 31 1
2 3 4 5 6 7 8
9 10 11 12 13 14 15
16 17 18 19 20 21 22
23 24 25 26 27 28 1
2 3 4 5 6 7 8

点击右上角即可分享
微信分享提示