【问题标题】:Recycler View list gets Jumbled回收商查看列表变得混乱
【发布时间】:2015-10-26 19:39:19
【问题描述】:

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

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;
    }
}
}

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

如何解决这个问题

谢谢

【问题讨论】:

    标签: android android-recyclerview


    【解决方案1】:

    删除关键字static 在您的ViewHolder 类中出现的所有位置。您需要每个ViewHolder 来管理RecyclerView 中单个项目的视图。就目前而言,使用static,您有多个ViewHolder 实例都使用相同的视图。

    【讨论】:

    • 非常感谢我明白了这个问题现在工作正常@CommonsWare
    【解决方案2】:

    而不是使用

    merchant= mDataset.get(position);
    

    你应该使用

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

    并根据您在绑定时的代码从位置中删除最终关键字,它修复了位置,并且回收器视图与视图持有者一起使用。

    【讨论】:

      猜你喜欢
      • 2018-05-03
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-24
      • 2021-12-31
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多