问题
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