通过动态数据绑定实现Imageview可见性

时间:2016-12-08 11:27:01

标签: android android-databinding

如果互联网连接不可用,我想要一个imageView可见,否则看不见。如何通过动态数据绑定实现这一目标?

让我们说,我的活动<?php $transactionDbModel = \App\Db\PaymentTransaction::find($transaction->id()); $transactionDbModel->payment_id = $payment->id(); $transactionDbModel->type = $transaction->type(); $transactionDbModel->amount = $transaction->amount()->value(); $transactionDbModel->currency = $transaction->amount()->currency(); $transactionDbModel->invoice_number = $transaction->invoiceNumber(); $transactionDbModel->reference = (string) $transaction->reference(); $transactionDbModel->status = $transaction->status(); $transactionDbModel->psp_reference = $transaction->pspReference(); $transactionDbModel->psp_status = $transaction->pspStatus(); $transactionDbModel->microtime = microtime(true); $transactionDbModel->save(); 中有一个函数,如果互联网连接可用,则返回true。如何动态地将返回值与imageView的可见性绑定?

1 个答案:

答案 0 :(得分:1)

您可以为ImageView使用自定义属性,并使用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);
  }
}