我正在尝试处理函数中的LiveData
值 ( profilePicture: Bitmap
),BindingAdapter
但第一个值始终为null。绑定适配器是一个ViewSwitcher
withProgressBar
和ImageView
。
像这样从firebase获取个人资料图片:
val downloadPictureResult = MutableLiveData<Bitmap>()
// ...
fun downloadProfilePic(): LiveData<Bitmap?> {
val imageRef = storage.getReference("images/$uid/profile/profile_picture.jpg")
imageRef.getBytes(ONE_MEGABYTE).addOnCompleteListener { task ->
if (task.isSuccessful) {
//...
downloadPictureResult.value = responseBitmap
//...
} else {
downloadPictureResult.value = null
Log.d(TAG, task.exception?.localizedMessage)
}
}
return downloadPictureResult;
}
因为第一个值是null而第二个是预期的位图对象,所以view.showNext()
被调用了两次。但对我来说更重要的是要理解为什么 firstvalue 为null,因为该setProfilePicture
方法会有更多的逻辑。
BindingAdapter 看起来像这样。
fun setProfilePicture(view: ViewSwitcher, profilePicture: Bitmap?) {
Log.d("PPSS", profilePicture.toString())
val imageView: ImageView = view[1] as ImageView
imageView.setImageDrawable(view.context.getDrawable(R.drawable.account_circle_24dp))
profilePicture.let { picture ->
if (picture != null) {
val rounded = RoundedBitmapDrawableFactory.create(view.resources, picture)
rounded.isCircular = true
imageView.setImageDrawable(rounded)
view.showNext()
} else {
view.showNext()
}
}
日志:
2019-04-13 17:53:01.658 11158-11158/... D/PPSS: null
2019-04-13 17:53:02.891 11158-11158/... D/PPSS: android.graphics.Bitmap@8b6ecb8