2015-10-26 105 views
0

我使用回收視圖來填充數組列表,回收站視圖列表被混亂

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

刪除關鍵字static它出現在你的無處不在班。您需要每個ViewHolder來管理RecyclerView中單個項目的視圖。現在,使用static,你有幾個ViewHolder實例工作在相同的意見。

+0

非常感謝我明白了這個問題現在工作Fine @ CommonsWare – Pragadees

1

而不是使用

merchant= mDataset.get(position); 

,你應該根據你的代碼上結合其固定的位置和回收視圖適用於查看持有者使用

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

,並從位置刪除final關鍵字。