【发布时间】:2020-06-16 14:22:20
【问题描述】:
添加逻辑以在 recyclerView 中以编程方式调整图像视图大小后,这些图像现在只会在刷新或滚动时出现。
onBindViewHolder 中调用的方法:
fun bindCommunityImageOffer(model: CommunityOffer) {
val requestListener = object : RequestListener<Drawable> {
override fun onLoadFailed(e: GlideException?, imageModel: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
Timber.d("bindCommunityImageOffer() - Binding cover image failed because ${e?.message}")
return false
}
override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean):Boolean{
Timber.d("bindCommunityImageOffer() - Binding cover image success")
return false
}
}
Interactors.glide.loadImage(model.coverUrl, communityImageIv, requestListener)
if (!model.mediaAspectRatio.isNullOrEmpty()) {
val aspectRatioSplit = model.mediaAspectRatio.split(":")
val widthRatio = Integer.parseInt(aspectRatioSplit[0])
val heightRatio = Integer.parseInt(aspectRatioSplit[1])
communityImageIv.scaleType = ImageView.ScaleType.FIT_XY
if (widthRatio == heightRatio) {
communityImageIv.layoutParams.height = communityImageIv.width
communityImageCl.layoutParams.height = communityImageCl.width
} else {
communityImageIv.layoutParams.height = ((communityImageIv.width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
communityImageCl.layoutParams.height = ((communityImageCl.width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
}
} else {
communityImageIv.scaleType = ImageView.ScaleType.CENTER_CROP
val widthRatio = 25
val heightRatio = 21
communityImageIv.layoutParams.height = ((communityImageIv.width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
communityImageCl.layoutParams.height = ((communityImageCl.width.toFloat() / widthRatio.toFloat()) * heightRatio).toInt()
}
}
被绑定的视图布局:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/communityImageCv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:elevation="16dp"
app:cardCornerRadius="10dp"
>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="@+id/communityImageCl"
android:layout_width="match_parent"
android:layout_height="0dp">
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/communityImageIv"
android:layout_width="match_parent"
android:layout_height="0dp"
android:scaleType="fitXY"
android:background="@color/transparent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:src="@color/alpha_grey" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
我的活动或 xml 布局中没有任何代码更改。我唯一添加的是以编程方式调整其大小的代码。这是如何打破绑定的?
注意:为不可见视图调用 onBindViewHolder,但顶部定义的 Glide 侦听器不会触发。
【问题讨论】:
标签: android android-recyclerview android-glide