通过动态数据绑定的Imageview可见性 [英] Imageview visibility by dynamic databinding

查看:62
本文介绍了通过动态数据绑定的Imageview可见性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果互联网连接不可用,我希望imageView是可见的,否则不可见.如何通过动态数据绑定实现这一目标?

I want an imageView to be visible if internet connectivity is unavailable, invisible otherwise. How do I achieve this by dynamic data-binding ?

可以说,我的活动checkInternetConnectivity中有一个函数,如果有Internet连接可用,则返回true.如何动态地将返回值与imageView的可见性绑定在一起?

Lets say, I have a function in my activity checkInternetConnectivity that returns true if internet connectivity is available. How do I dynamically bind the return value with the visibility of the imageView ?

推荐答案

您可以为ImageView使用自定义属性,并使用Binding Adapter动态调用该方法.

You can use a custom attribute for the ImageView and invoke the method dynamically using Binding Adapter .

XML

<ImageView android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    app:internet="@{imageUrl}"/>

JAVA代码

@BindingAdapter({"bind:internet")
public static void loadImage(ImageView view, String url) {
  if(checkInternetConnectivity()) {
     // load the image
  }
  else {
     // setting the view visibility to invisible
     view.setVisibility(View.INVISIBLE);
  }
}

这篇关于通过动态数据绑定的Imageview可见性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆