RecyclerView 不显示图像全高
RecyclerView not showing the image full height
我对 recyclerview 中的图像有疑问
They shown like this
我加载图像的代码是:
@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}
fun downloadBitmap(imageUrl: String): Bitmap? {
return try {
val conn = URL(imageUrl).openConnection()
conn.connect()
val inputStream = conn.getInputStream()
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream.close()
bitmap
} catch (e: Exception) {
Log.e(ContentValues.TAG, "Exception $e")
null
}
}
XML 项目:
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_song_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:imageUrl="@{songObject.songImage}"
android:src="@drawable/img_song_cover" />
和XML对于回收商:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home_albums_list"
android:layout_width="match_parent"
android:layout_height="@dimen/dp200w"
android:layout_marginStart="@dimen/dp16w"
android:layout_marginLeft="@dimen/dp16w"
android:layout_marginTop="@dimen/dp8w"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_home_albums_title"
tools:listitem="@layout/item_album" />
我的任务是在不使用任何第三方的情况下完成所有工作,
那么有什么帮助吗?
我已经解决了这个问题,只需添加一条语句来检查之前加载的图像是否在我使用回收器视图滚动时不会多次渲染它,并删除占位符。
if(this.drawable == null) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}
我对 recyclerview 中的图像有疑问 They shown like this
我加载图像的代码是:
@BindingAdapter("imageUrl")
fun ImageView.bindImage(imgUrl: String?) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}
fun downloadBitmap(imageUrl: String): Bitmap? {
return try {
val conn = URL(imageUrl).openConnection()
conn.connect()
val inputStream = conn.getInputStream()
val bitmap = BitmapFactory.decodeStream(inputStream)
inputStream.close()
bitmap
} catch (e: Exception) {
Log.e(ContentValues.TAG, "Exception $e")
null
}
}
XML 项目:
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/iv_song_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
app:imageUrl="@{songObject.songImage}"
android:src="@drawable/img_song_cover" />
和XML对于回收商:
<androidx.recyclerview.widget.RecyclerView
android:id="@+id/rv_home_albums_list"
android:layout_width="match_parent"
android:layout_height="@dimen/dp200w"
android:layout_marginStart="@dimen/dp16w"
android:layout_marginLeft="@dimen/dp16w"
android:layout_marginTop="@dimen/dp8w"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/tv_home_albums_title"
tools:listitem="@layout/item_album" />
我的任务是在不使用任何第三方的情况下完成所有工作, 那么有什么帮助吗?
我已经解决了这个问题,只需添加一条语句来检查之前加载的图像是否在我使用回收器视图滚动时不会多次渲染它,并删除占位符。
if(this.drawable == null) {
imgUrl?.let {
val imgUri = imgUrl.toUri().buildUpon().scheme("https").build().toString()
val uiHandler = Handler(Looper.getMainLooper())
thread(start = true) {
val bitmap = downloadBitmap(imgUri)
uiHandler.post {
this.setImageBitmap(bitmap)
}
}
}
}