java如何在安卓中创建自定义数据绑定?(安卓工作室)
我想用下面的代码实现从ImageView
下载图像的自定义函数,如app:imageUrl="@{status.imageUrl}"
:
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:安卓="http://schemas.安卓.com/apk/res/安卓"
xmlns:app="http://schemas.安卓.com/apk/res-auto">
<data>
<variable
name="status"
type="com.databinding.data.Status" />
</data>
<RelativeLayout
安卓:id="@+id/status_container"
安卓:layout_width="match_parent"
安卓:layout_height="match_parent">
<ImageView
安卓:id="@+id/status_avatar"
安卓:layout_width="64dp"
安卓:layout_height="64dp"
安卓:layout_alignParentLeft="true"
安卓:layout_alignParentStart="true"
安卓:layout_alignParentTop="true"
安卓:contentDescription="@null"
app:imageUrl="@{status.imageUrl}"/>
</RelativeLayout>
</layout>
如何编写这个可以从@{status.imageUrl}
自动下载图像的函数?
使用此库com.安卓.databinding
# 1 楼答案
以下是我喜欢的:
首先创建一个自定义类扩展表单图像视图
第二,在res文件夹中声明一个可设置样式的文件
最后让我们制作下载程序
现在,您可以轻松地在xml中添加链接
# 2 楼答案
对于这项工作,您需要一个类似android databinding lib的库
在此库中,首先将以下脚本添加到项目的
build.gradle
:并将这些代码添加到模块文件的
build.gradle
顶部:创建类,例如:
class BindingCustom
并编写以下代码:在
BindingCustom
类中,您有loadImage
方法以感兴趣的方式从URL下载图像,但我使用Picasso库,因为它是此作业的公共库,您可以将其更改为代码This is a helpful link for more information