Reputation: 71
I am using recycler view to populate a array list ,
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;
}
}
}
When I Scroll to Bottom of the List My order gets changed when I scroll back to top , Some times even same Item gets displayed many times
How to solve this problem
Thanks
Upvotes: 0
Views: 425
Reputation: 5261
Instead of using
merchant= mDataset.get(position);
you should use
merchant= mDataset.get(holder.getAdapterPosition());
and remove final keyword from the position as according to your code on binding it fixes the position and recycler view works with view holder.
Upvotes: 1
Reputation: 1006869
Remove the keyword static
everywhere it appears in your ViewHolder
class. You need each ViewHolder
to manage the views for a single item in the RecyclerView
. As it stands, using static
, you have having several ViewHolder
instances all work with the same views.
Upvotes: 2