我已经使用@bindingAdapter在我的布局中设置Avatar,但是当更改某些值时,bindingAdapter
不会加载图像。这是我的viwmodel
class ProfileViewModel constructor(val repository: ProfileRepository, val mContext: Context, photo: Photo = Photo("")): ViewModel() {
var avatar: ObservableField<String> = ObservableField("")
var responseListener: ResponseListener? = null
var fileUploadEvent: FileEvents? = null
fun getAvatarUrl() : ObservableField<String> {
return avatar
}
companion object {
@BindingAdapter("avatarUrl")
@JvmStatic
fun loadAvatar(imageView: ImageView, avatarUrl: String) {
Glide.with(imageView.context)
.load(avatarUrl)
.placeholder(R.mipmap.profile)
.apply(RequestOptions.circleCropTransform())
.into(imageView)
}
}
fun onProfileImageUpload(uri: Uri) {
fileUploadEvent!!.onUpload()
Coroutines.main {
try {
val imageResponse = repository.updateProfileAvatar(
getImageForUpload(uri, "avatar")
)
avatar.set(imageResponse.data.avatar.thumbnail)
fileUploadEvent!!.onUploadSuccess(avatar.get()!!)
} catch(e : Exception) {
fileUploadEvent!!.onUploadFailure(e.toString())
}
}
}
}
在我的XML中,我像这样定义了命名空间
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="viewmodel"
type="com.kreeti.gogal.ui.profile.ProfileViewModel" />
</data>
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="@+id/profile_root"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:id="@+id/welcome_header"
style="@style/glacial_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="@string/welcome"
android:textColor="@color/violet"
android:textSize="32sp" />
<LinearLayout
android:id="@+id/profile_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:orientation="horizontal">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="3"
>
<ImageView
android:id="@+id/avatar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:scaleType="center"
android:onClick="@{viewmodel::onAvatarClick}"
app:avatarUrl="@{viewmodel.avatarUrl}" />
<ProgressBar
android:id="@+id/avatar_progress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:visibility="gone" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
</ScrollView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
</layout>
我正在从api获取照片网址,我想在通过视图绑定获取照片时更改照片。更改网址时如何加载图像?我需要听一些吗?