在我的应用程序中,我正在展示从Web服务中获取的文章.那些文章有标题,图像和其他领域.为了显示图像,我正在使用毕加索.
问题 - 在滚动RecyclerView时,我看到图像放错了位置.有些项目有重复的图像.我知道它是RecyclerView重用xml项目布局,因此它产生了一个问题.但我认为必须解决这个问题.我已经google了这个,并在stackoverflow上发现了一些帖子,但这没有帮助.例如-
毕加索将图片加载到列表适配器中的错误图像视图中
我尝试过以下但有同样的问题 -
public class ArticleAdapter extends RecyclerView.Adapter{ protected Activity mActivity; protected List mItems = new ArrayList<>(); private static OnEntityClickCallback mCallback; public ArticleAdapter(Activity pActivity) { mActivity = pActivity; } public void setItemClickCallback(OnEntityClickCallback pCallback) { this.mCallback = pCallback; } @Override public ArticleViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { View articleView = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_article_item, parent, false); return new ArticleViewHolder(articleView); } public void addItem(List moreItems) { mItems.addAll(moreItems); notifyDataSetChanged(); } public void removeItems(){ mItems.clear(); notifyDataSetChanged(); } @Override public void onBindViewHolder(ArticleViewHolder holder, int position) { holder.bind(mItems.get(position)); } @Override public int getItemCount() { return mItems.size(); } static class ArticleViewHolder extends RecyclerView.ViewHolder { private TextView tvTitle; TextView tvAuthor; TextView tvPostdate; ImageView imgImage; RatingBar ratingBar; public ArticleViewHolder(View view) { super(view); this.tvTitle = (TextView) view.findViewById(R.id.tv_title); this.tvAuthor = (TextView) view.findViewById(R.id.tv_author); this.tvPostdate = (TextView) view.findViewById(R.id.tv_date); this.imgImage = (ImageView) view.findViewById(R.id.img_image); this.ratingBar = (RatingBar) view.findViewById(R.id.ratingBar); } public void bind(final Article article) { tvTitle.setText(article.getTitle()); tvPostdate.setText(article.getPostdate()); tvAuthor.setText(article.getAuthor()); // Canceling the older request Picasso.with(imgImage.getContext()).cancelRequest(imgImage); // Creating a new request. if (article.getImage() != null) { Picasso.with(imgImage.getContext()).load(article.getImageUrl()) .placeholder(R.drawable.noimage) .error(R.drawable.noimage) .into(imgImage); } this.itemView.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (mCallback != null) mCallback.onEntitySelected(article); } }); } } }
更新 - 我的布局文件的一部分
任何帮助,将不胜感激.谢谢.
外部设置drawable,因为你正在获得已经附加了一些drawable的imageview.Imageview没有重新创建,因此ImageView中的上一个图像将被删除,直到您将其删除.
if (article.getImage() != null) { Picasso.with(imgImage.getContext()).load(article.getImageUrl()) .placeholder(R.drawable.noimage) .error(R.drawable.noimage) .into(imgImage); }else{ // imgImage.setImageDrawable(null); imgImage.setImageDrawable(R.drawable.some_drawable); }