这是我的代码
当我上下滚动页面时,图片会在其他位置添加,即使在那些位置上的图片路径不存在。假设图片路径在1、2和3位置,但它也会在4、10、15等位置显示图片。
public class Contact_Adapter_RecyeckView extends
RecyclerView.Adapter<Contact_Adapter_RecyeckView.ViewHolder> {
private ArrayList<ContactBean> arraylist;
private Context context;
public Contact_Adapter_RecyeckView(Context context,
ArrayList<ContactBean> arrayListImages) {
this.arraylist = arrayListImages;
this.context = context;
}
// Create new views (invoked by the layout manager)
@Override
public Contact_Adapter_RecyeckView.ViewHolder onCreateViewHolder(
ViewGroup parent, int viewType) {
// create a new view
View itemLayoutView = LayoutInflater.from(parent.getContext()).inflate(
R.layout.contact_adapter_recycleview, null);
// create ViewHolder
ViewHolder viewHolder = new ViewHolder(itemLayoutView);
return viewHolder;
}
// Replace the contents of a view (invoked by the layout manager)
@Override
public void onBindViewHolder(ViewHolder viewHolder, int pos) {
String image = arraylist.get(pos).getImage();
File imgFile = new File(image);
if(imgFile.exists())
{
viewHolder.ivProfile.setImageURI(Uri.fromFile(imgFile));
}
//viewHolder.ivProfile.setImageBitmap(BitmapFactory.decodeFile(arraylist.get(pos).getImage()));
//viewHolder.ivProfile.setVisibility(View.VISIBLE);
// inner class to hold a reference to each item of RecyclerView
public static class ViewHolder extends RecyclerView.ViewHolder {
public ImageView ivProfile;
public View view;
public CardView cardview;
public ViewHolder(View itemLayoutView) {
super(itemLayoutView);
ivProfile = (ImageView) itemLayoutView
.findViewById(R.id.iv_user_profile);
}
}
// Return the size of your itemsData (invoked by the layout manager)
@Override
public int getItemCount() {
return arraylist.size();
}}
我看到了这个链接,以及其他一些链接,但问题仍然存在在改变时滚动,找不到原因。
imgFile.exists()
返回了false
,尝试在 else 块中设置默认图像。 - Rami