android recyclerview imageview从URL显示不正确的图像

时间:2019-07-02 04:22:43

标签: java android

这是我从url设置图像的适配器类

public class EmployeeAdapter extends RecyclerView.Adapter<EmployeeAdapter.MyViewHolder>
        implements Filterable {
    private Context context;
    private List<EmployeeList.details>detailsList;

    public class MyViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {
        public CircleImageView image;

        private MyViewHolder(View view) {
            super(view);
            image = view.findViewById(R.id.employeeImg);           
        }

    public EmployeeAdapter(Context context, List<EmployeeList.details> details) {
        this.context = context;
        this.detailsList = details;
    }

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View itemView = LayoutInflater.from(parent.getContext())
                .inflate(R.layout.employee_item, parent, false);
        return new MyViewHolder(itemView);
    }

    @NonNull
    @Override
    public void onBindViewHolder(MyViewHolder holder, final int position) {
        final EmployeeList.details details = detailsList.get(position);

        String strUserImg=detailsList.get(position).getImage();

        if(strUserImg.length()>0){
            Glide.with(context)
                    .load(strUserImg)
                    .into(holder.image);
        }
    }
    @Override
    public int getItemCount() {
        return detailsList.size();
    } 
}

我正在尝试从imageurl设置circleimageview的图像。图像也从url加载,并且它在circleimageview中显示,但是问题是当我从url滚动图像后上下滚动时改变了位置。网址

例如:第三位置图像显示在第一位置。

请帮助我解决问题。

4 个答案:

答案 0 :(得分:1)

一个非常简单的解决方案是在if块中添加else条件

if(strUserImg.length()>0){
            Glide.with(context)
                    .load(strUserImg)
                    .into(holder.image);
} else {
      Glide.with(context)
                    .load(<PLACEHOLDER_IMAGE>)
                    .into(holder.image);


}

答案 1 :(得分:0)

    final EmployeeList.details details = detailsList.get(position);
    String strUserImg=details.getImage();

    if(strUserImg.length()>0){
        Glide.with(context)
                .load(strUserImg)
                .into(holder.image);
    } else {
        Glide.with(context)
                .load(<YOUR_PLACEHOLDER_IMAGE>)
                .into(holder.image);
    }

答案 2 :(得分:0)

问题出在您的if(strUserImg.length()>0){上,如果strUserImg.length() <= 0,则代码将简单地重用以前的视图持有者并加载相同的图像。尝试使用Glide.clear

if(strUserImg.length()>0){
    Glide.with(context)
        .load(strUserImg)
        .into(holder.image);
} else {
    Glide.clear(holder.image);
}

答案 3 :(得分:0)

if(strUserImg!=null){
    Glide.with(context)
        .load(strUserImg)
        .into(holder.image);
} else {
     Glide.with(context)
        .load(your_place_holder)
        .into(holder.image);
}