【问题标题】:RecyclerView scroll up is not smooth when the height isn't fixed高度不固定时RecyclerView向上滚动不流畅
【发布时间】:2018-06-12 04:22:47
【问题描述】:

如问题所述,我有一个 RecyclerView 可以显示不同高度的图像。

ImageView 的初始高度为 0dp,宽度为 match_parent,但在 BindViewHolder 中我将高度设置为正确的值。

如果您向下滚动列表,它可以正常工作,但是当您向上滚动时: 绘制之前的 ViewHolder 时,它的高度最初设置为 40dp 之类的值,因为图像尚未加载,但随着新高度设置为 ImgeView,它突然跳到 400,这使得它非常抖动且不平滑全部。

我尝试了预取和缓存,但没有任何效果。

如果知道显示的项目数将达到 1000,我应该使用 ListView 并按需加载图像,还是我该怎么办?

是 ViewHolder 布局

<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardElevation="8dp"
app:cardCornerRadius="8dp"
android:layout_margin="8dp">

<android.support.constraint.ConstraintLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content">

    <com.davemorrissey.labs.subscaleview.SubsamplingScaleImageView
        android:id="@+id/post_image"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintTop_toTopOf="parent"
        android:visibility="gone"/>
</android.support.constraint.ConstraintLayout>

这是 OnBindViewHolder

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    val target: SimpleTarget<File> = object: SimpleTarget<File>() {
        override fun onResourceReady(resource: File, transition: Transition<in File>?) {
            thumbnail.setImage(ImageSource.uri(Uri.fromFile(resource)).tilingEnabled())
        }
    }
    if (post?.url?.endsWith(".jpg", true) == true|| post?.url?.endsWith(".png", true) == true) {
        thumbnail.visibility = View.VISIBLE
        thumbnail.recycle()
        thumbnail.setDoubleTapZoomDuration(300)
        if(post.preview != null) {
            thumbnail.post {
                val scale: Float = post.preview.images[0].source.height.toFloat()/post.preview.images[0].source.width.toFloat()
                val layoutParams: ViewGroup.LayoutParams = thumbnail.layoutParams
                layoutParams.height = (thumbnail.measuredWidth.toFloat() * scale).toInt()
                thumbnail.layoutParams = layoutParams
                glide.downloadOnly().load(post.preview.images[0].source.url).into(target)
            }
        }
    } else {
        thumbnail.visibility = View.GONE
        glide.clear(target)
        thumbnail.recycle()
    }
    }
}

缩略图是SSIV

【问题讨论】:

  • 你能粘贴代码吗?例如。适配器类和 ViewHolders
  • 已更新代码。
  • 这些图片的高度范围是多少?它们变化很大吗?您能否尝试使用默认大小的占位符图像并将其替换为 onResourceReady()?
  • 变化很大,比例实际上可能是 0.1 或 10。我尝试将高度设置为 400dp 以接近它们的高度,它使事情变得更好,但仍然紧张。
  • 没关系,我找到了灵魂,我会发布答案。感谢您的帮助。

标签: android android-recyclerview


【解决方案1】:

问题是我每次都异步获取测量的宽度,导致设置高度有延迟。

我在 OnCreateViewHolder 上计算了 measureHeight 并将其存储以备后用。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-05-29
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多