Imageview visibility by dynamic databinding

元气小坏坏 提交于 2019-12-07 12:57:49

问题


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

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 ?


回答1:


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 code

@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);
  }
}


来源:https://stackoverflow.com/questions/41038357/imageview-visibility-by-dynamic-databinding

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!