【问题标题】:Loading picture to correct position in recycler view将图片加载到回收站视图中的正确位置
【发布时间】:2018-05-13 04:27:47
【问题描述】:

我在我的回收器适配器中进行网络调用以检索图片的 url。收到 url 后,我使用通用图像加载器将图片加载到图像视图中。问题是当我不滚动图片时,图片被加载到正确的位置,但是一旦我滚动图片就会在错误的地方膨胀。 这是我的适配器:

override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
    if (holder is ViewHolder) {
        val article = feeds[position]

        holder.articleTitle.setFont("SourceSansPro-SemiBold.ttf")
        holder.articleDescription.setFont("OpenSans-Regular.ttf")
        holder.articleTime.setFont("OpenSans-Light.ttf")

        mAnimator?.onBindViewHolder(holder.itemView, position)
        holder.apply {
            article.apply {
                articleTitle.text = title
                articleDescription.text = Html.fromHtml(description)
                articleTime.text = TimeUtils.convertLongToTime(pubDate)

                if (image.isBlank()){
                    //load picture url when it's empty
                    mContext?.doAsync {
                        ImageExtractor.extractImageUrl(link, object : OnImageExtractorListener {
                            override fun onSuccess(url: String) {
                                v("imaaaage success $title $url")
                                mContext?.runOnUiThread {
                                    article.image = url

                                    //use uil to load the image didn't work so I tried just updating the model
                                    //articleImage.displayImage(url)

                                    feeds[position] = article
                                    notifyItemChanged(position)
                                }
                                val dbo = context.getDatabase()
                                dbo.updateArticleImage(dbo,url,article.id)
                            }

                            override fun onError() {
                            }
                        })
                    }
                }else{
                    articleImage.displayImage(image)
                    isRead?.let {
                        if (isRead!! && !isSaved){
                            grayScale(holder)
                        }
                    }
                }

                container.setOnClickListener {
                    itemClick(this)
                    if (!isSaved){
                        article.isRead = true
                        feeds[position] = article
                        notifyItemChanged(position)
                    }
                }
            }
        }
    }else if (holder is LoadingViewHolder){
        holder.progressBar.isIndeterminate = true
    }
}

如果用户滚动与否,我需要一种将图像加载到正确位置的方法。

【问题讨论】:

  • 尝试使用任何图像加载器库,如 glide、fresco 等,并使用查看器维护您的图像 url
  • @ArbazRizvi 我会尝试另一个库,但我目前正在使用UIL

标签: android kotlin android-recyclerview universal-image-loader


【解决方案1】:

RecyclerView 将重用 ViewHolders 以减少通货膨胀和内存使用。

正确的做法是在onBindViewHolder中清空View的旧状态(设置图片为null),设置新的。

确保在它出现的所有时候都清除它绑定(在测试之前)

articleImage.displayImage(null)
if (image.isBlank()){

由于下载是异步任务,绑定时不会妨碍,这就是为什么你只需清除旧的,对于文本,它可以立即设置。

【讨论】:

  • setImageBitmap(null) 在下载新的之前
  • 谢谢你试试看
【解决方案2】:

考虑使用库进行异步图像加载,例如Picasso。一切都为您处理,例如缓存、占位符...

在您的适配器中:

Picasso.with(context).load("url")
.placeholder(R.drawable.user_placeholder).into(imageView);

分级:

compile 'com.squareup.picasso:picasso:2.5.2'

就是这样!

【讨论】:

    【解决方案3】:

    你需要把 setHasStableIds(true);在您的适配器的构造函数中并放入:

    @Override
    public int getItemViewType(int position) {
        return position;
    }
    
    @Override
    public long getItemId(int position) {
        return position;
    }
    

    因此,即使在滚动之后,它也会将所有图像保持在准确的位置。

    【讨论】:

      猜你喜欢
      • 2018-11-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-05-30
      • 2022-01-10
      • 2017-10-05
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多