适配器在快速滚动时更改图像

时间:2016-10-06 06:31:10

标签: android android-recyclerview adapter

嘿伙计我这个适配器检索从Web服务填充的新闻列表,它工作得很好,但问题是当我快速滚动图像(看不见的行)被替换时,你能帮忙吗? ?

这是我的适配器:

public class NewsAdapter extends RecyclerView.Adapter<NewsAdapter.RecyclerViewHolder> {

private List<New> news;
private Activity context;
private RecyclerViewOnItemClickListener itemClickListener;
private boolean tabletSize;

public NewsAdapter(Activity context, ArrayList<New> newss, RecyclerViewOnItemClickListener itemClickListener, boolean tabletSize) {
    this.news = newss;
    this.context = context;
    this.itemClickListener = itemClickListener;
    this.tabletSize = tabletSize;
}


@Override
public void onBindViewHolder(final RecyclerViewHolder holder, int position) {

    holder.title.setText(news.get(position).getTitle());
    //holder.date.setText(news.get(position).getPublishDate());


    String date = news.get(position).getPublishDate().substring(0,news.get(position).getPublishDate().indexOf("T"));
    /*Date date2= null;
    DateFormat formatter = new SimpleDateFormat("yyyy-MM-DD");
    try {
        date2 = (Date) formatter.parse(date);
    }catch (Exception e){

    }
    SimpleDateFormat newFormat = new SimpleDateFormat("dd-MM-yyyy");
    String finalString = newFormat.format(date2);*/
    if (MyApplication.lang.equals("en")) {
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d");
        Date date3 = null;
        try {
            date3 = sdf.parse(date);
        } catch (Exception e) {

        }
        sdf = new SimpleDateFormat("EEEE, MMMM yyyy");
        String format = sdf.format(date3);
        System.out.print("Result: " + format);
        holder.date.setText(format);
    }else{
        Locale locale = new Locale("ar");
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-d");
        Date date3 = null;
        try {
            date3 = sdf.parse(date);
        } catch (Exception e) {

        }
        sdf = new SimpleDateFormat("EEEE, MMMM yyyy",locale);
        String format = sdf.format(date3);
        System.out.print("Result: " + format);
        Log.wtf("result",format);
        holder.date.setText(format);
        //holder.date.setText(date);
    }




    if(news.get(position).getPictures().size()!=0)

        Picasso.with(context)
                .load(news.get(position).getPictures().get(0).getFilePath()).placeholder(R.drawable.defaultbg)
                //.resize(250,200)
                //.rotate(90)
                .into(holder.img, new com.squareup.picasso.Callback() {
                    @Override
                    public void onSuccess() {
                        //do smth when picture is loaded successfully
                        holder.img.setAlpha((float)1);
                    }

                    @Override
                    public void onError() {
                        //progress.setVisibility(View.GONE);
                        //do smth when there is picture loading error
                    }
                });

}

@Override
public int getItemCount() {
    return this.news.size();
}


public interface RecyclerViewOnItemClickListener {
    void onItemClicked(View v, int position);
}

public RecyclerViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View layoutView;

        layoutView = LayoutInflater.from(parent.getContext()).inflate(R.layout.news_list_item, parent,false);
        RecyclerViewHolder rcv = new RecyclerViewHolder(layoutView);
        return (rcv);
}

public class RecyclerViewHolder extends RecyclerView.ViewHolder implements View.OnClickListener {

    public TextView title,date;
    public ImageView img;
    protected  View itemView;
    LinearLayout rel;

    public RecyclerViewHolder(View itemView) {
        super(itemView);
        this.itemView = itemView;
        this.itemView.setOnClickListener(this);

        title=(TextView)itemView.findViewById(R.id.title);
        date=(TextView)itemView.findViewById(R.id.tvDate);
        img=(ImageView)itemView.findViewById(R.id.img);
        rel=(LinearLayout)itemView.findViewById(R.id.background);
        Log.wtf("received log",""+tabletSize);
        if(!tabletSize)
            Actions.setViewResizingListRow(itemView,context);
        else
            Actions.setTabletViewResizingListRow(itemView,context);
        if(MyApplication.lang.equals("en")) {
            title.setTypeface(MyApplication.opensanregular);
            date.setTypeface(MyApplication.opensanslight);

        }
        else
            date.setTypeface(MyApplication.gdenartwolight);
        if (MyApplication.lang.equals("ar")) {
            rel.setLayoutDirection(View.LAYOUT_DIRECTION_RTL);
        }
    }

    @Override
    public void onClick(View view) {
        itemClickListener.onItemClicked(view, getLayoutPosition());
    }
}
}

1 个答案:

答案 0 :(得分:0)

在活动中设置适配器后放置此行

yourRecyclerView.setItemAnimator(null);