IrA*_*App 23 android android-databinding
我很新Android Data Binding.我正在学习本教程:数据绑定库.我正在尝试使用接收多个参数的适配器.这是我的代码:
XML
<ImageView
android:layout_width="@dimen/place_holder_size"
android:layout_height="@dimen/place_holder_size"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_centerVertical="true"
app:url="@{image.imageUrl}"
app:size="@{@dimen/place_holder_size}"
/>
Run Code Online (Sandbox Code Playgroud)
绑定适配器类
public class ViewBindingAdapters extends BaseObservable {
@BindingAdapter({"bind:url", "bind:size"})
public static void loadImage(ImageView imageView, String url, int size) {
if (!Strings.isNullOrEmpty(url)) {
Picasso.with(imageView.getContext()).load(url).resize(size, size).centerCrop().into(imageView);
}
}
....
}
Run Code Online (Sandbox Code Playgroud)
但是我收到了这个错误:
java.lang.RuntimeException:发现数据绑定错误.****/数据绑定错误****消息:在android.widget.ImageView上找不到参数类型为java.lang.String的属性'app:url'的setter.file:... li_image_item.xml loc:30:27 - 30:40****\data binding error****
有谁知道为什么?
提前致谢!
Rav*_*avi 34
问题是@dimen/place_holder_size,float当你抓住它时它会返回int
改变你的BindingAdapter方法
@BindingAdapter({"bind:url", "bind:size"})
public static void loadImage(ImageView imageView, String url, float size) {
}
Run Code Online (Sandbox Code Playgroud)
你可以参考这个
Kis*_*nki 11
What I did wrong is the order of the arguments in the function. You can add multiple attributes in Binding Adapter, but they should match the arguments with the same sequence defined in the method.
Here is my code snippet for Kotlin
@BindingAdapter(value = ["bind:brand", "bind:model", "bind:age"], requireAll = false)
@JvmStatic
fun bindProductDetails(linearLayout: LinearLayout, brand: String?, model: String?, age: String?) {
if (brand != null && !brand.isEmpty()) {
//code
//code
}
}
Run Code Online (Sandbox Code Playgroud)
尝试这个
@BindingAdapter(value={"url", "size"}, requireAll=false)
public static void loadImage(ImageView imageView, String url, int size) {
if (!Strings.isNullOrEmpty(url)) {
Picasso.with(imageView.getContext()).load(url).resize(size, size).centerCrop().into(imageView);
}
}
Run Code Online (Sandbox Code Playgroud)
您不需要创建 prefix bind:,只需使用它。
@BindingAdapter({"url", "size"})
public static void loadImage(ImageView imageView, String url, float size) {
}
Run Code Online (Sandbox Code Playgroud)
在 xml 中使用任何前缀,如 app:
app:url="@{image.imageUrl}"
Run Code Online (Sandbox Code Playgroud)
除了@Kishan Solanki 上面提供的示例,随着数据绑定库中发生的变化,您只需要用逗号分隔值声明它们。例子
@BindingAdapter("loadImageFrom", "widthDp")
fun loadImages(imageView: ImageView, url: String?, widthDp: Int) {
url?.let {
val height = (widthDp / 3) * 4
val fullUrl = getImagePosterUrl(url)
Picasso.get().load(fullUrl).resize(widthDp.px, height.px).into(imageView)
}
}
Run Code Online (Sandbox Code Playgroud)
但是要记住的最重要的事情是您以 DataBinding 格式向适配器属性提供数据。
在 XML 中提供 Int 作为参数的示例,您必须编写
app:widthDp="@{120}"
Run Code Online (Sandbox Code Playgroud)
这是
app:your_attribute="@{your_data}"
PS - 还有一件事,widthDp**.px** 是一个扩展函数,它Int根据屏幕密度将dp 值转换为相关像素值。所以不要混淆。
请参阅https://developer.android.com/topic/libraries/data-binding/binding-adapters。
@BindingAdapter("imageUrl", "placeholder", requireAll = false)
fun ImageView.setImageUrl(url: String?, placeHolder: Drawable?) {
if (url == null) {
setImageDrawable(placeholder)
} else {
MyImageLoader.loadInto(this, url, placeholder)
}
}
Run Code Online (Sandbox Code Playgroud)
请注意,BindingAdapter属性和setImageUrl参数可能具有不同的名称。但它们应该按照相同的顺序进行。如果其中一些属性是在 XML 中定义的(在本例中是ImageView),则将调用此方法。
<ImageView
app:imageUrl="@{venue.imageUrl}"
app:placeholder="@{@drawable/venueError}" />
Run Code Online (Sandbox Code Playgroud)
属性应具有与方法中定义的类型相同的类型。您还可以使用侦听器。使用接口代替 Kotlin lambda 。
该类应与 XML 放在同一模块中。否则你会得到类似的错误AAPT: error: attribute imageUrl (aka com.example.imageUrl) not found.。
| 归档时间: |
|
| 查看次数: |
14927 次 |
| 最近记录: |