Recycler View列表变得混乱

时间:2015-10-26 19:39:19

标签: android android-recyclerview

我正在使用回收站视图来填充数组列表

public class Recycleradapter extends RecyclerView.Adapter<Recycleradapter.ViewHolder>  {
private ArrayList<Merchant> mDataset = new ArrayList<>();
private Context mContext;
private Handler mHandler;
private int lastPosition = -1;
private Handler callHandler;
public Recycleradapter(ArrayList<Merchant> list,Context context,Handler handler,Handler callHandler)
{
    mContext = context;
    mHandler = handler;
    mDataset = list;
    this.callHandler = callHandler;
}

@Override
public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    View v = LayoutInflater.from(parent.getContext())
            .inflate(R.layout.merchantcard, parent, false);
    ViewHolder viewHolder = new ViewHolder(v);
    return viewHolder;
}

@Override
public void onBindViewHolder(ViewHolder holder, final int position) {
    Merchant merchant = new Merchant();
    merchant= mDataset.get(position);
    holder.MerchantName.setText(merchant.MerchantName);
    holder.Address.setText(merchant.Address1);
    if(merchant.Distance!= null)
    {
        if(merchant.Distance>1000)
        {
            merchant.Distance= merchant.Distance/1000;
            holder.Distance.setText(new DecimalFormat("##.##").format(merchant.Distance)+" km");
        }else
        {
            holder.Distance.setText(new DecimalFormat("##.##").format(merchant.Distance)+" m");
        }
    }


    holder.cardView.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            Message message = mHandler.obtainMessage();
            Bundle b = new Bundle();
            b.putInt("pos", position);
            message.setData(b);
            message.sendToTarget();
        }
    });


}

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

public static class ViewHolder extends RecyclerView.ViewHolder {
 public static TextView MerchantName,Address,Distance;
    public  static CardView cardView;
    public  static ImageView call;

    public ViewHolder(View itemView) {
        super(itemView);
        MerchantName = (TextView)itemView.findViewById(R.id.MerchantName);
        Address = (TextView)itemView.findViewById(R.id.MerchantAddressline1);
        cardView = (CardView)itemView.findViewById(R.id.MerchantCard);
        call =(ImageView)itemView.findViewById(R.id.merchant_call);
        Distance= (TextView)itemView.findViewById(R.id.distance_merchant_card);


    }
}
private void setAnimation(View viewToAnimate, int position)
{
    // If the bound view wasn't previously displayed on screen, it's animated
    if (position > lastPosition)
    {
        Animation animation = AnimationUtils.loadAnimation(mContext, android.R.anim.slide_in_left);
        viewToAnimate.startAnimation(animation);
        lastPosition = position;
    }
}
}

当我滚动到列表的底部时当我滚动到顶部时,我的订单会发生变化,有时甚至同一个项目会多次显示

如何解决这个问题

谢谢

2 个答案:

答案 0 :(得分:2)

删除static课程中出现的关键字ViewHolder。您需要每个ViewHolder来管理RecyclerView中单个项目的观看次数。就目前而言,使用static,您有多个ViewHolder个实例都使用相同的视图。

答案 1 :(得分:1)

而不是使用

merchant= mDataset.get(position);

你应该使用

merchant= mDataset.get(holder.getAdapterPosition());

并根据绑定时的代码从位置中删除final关键字,修复位置和回收站视图与视图持有者一起使用。