TextViews 在滚动时回收它们的位置和标题,Glide Gallery

TextViews recycle their positions and title when scrolling, Glide Gallery

我正在尝试使用 Glide Image Library 在 gridview 中加载图像并使用 REccycler View。但是,我希望在每个图像下方都有一个标题 TextView。

我为此设置了自定义布局,并尝试在 onBindViewHolder 中加载 TextView。这行得通,但在滚动时,位置被回收,TextViews 标题被更改,弄得一团糟。

@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
  Image image = images.get(position);

//        // setup Glide request without the into() method
//        DrawableRequestBuilder<String> thumbnailRequest = Glide
//                .with(mContext)
//                .load(image.getMedium());

    // pass the request as a a parameter to the thumbnail request
    Glide.with(mContext).load(image.getMedium())
            .thumbnail(0.3f)
            .crossFade()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(holder.thumbnail);


     titleText.setText(image.getName());

}

我该如何解决这个亲爱的开发者?

我认为问题是 titleText 没有存储在 ViewHolder 中。

将其保存在您的 holder 对象中并使用:

holder.titleText.setText(image.getName());

这将随着 holder 的变化相应地改变 TextView。否则会因为支架被回收而变得一团糟。

类似于:

class MyViewHolder extends RecyclerView.ViewHolder{
TextView titleText;
ImageView thumbnail;
.
.
.



@Override
public void onBindViewHolder(MyViewHolder holder, int position) {
Image image = images.get(position);


Glide.with(mContext).load(image.getMedium())
        .thumbnail(0.3f)
        .crossFade()
        .diskCacheStrategy(DiskCacheStrategy.ALL)
        .into(holder.thumbnail);


 holder.titleText.setText(image.getName());

}
}