androidandroid-recyclerview

How to keep Recyclerview items at same position?


I have the following issue. I populate a Recyclerview from my Fragment class. So far everything works out. However when I test my app and scroll up and down the populated recycler list the contents of each item change a.k.a. they get recycled...

How can I save each item's position and restore its content to the same position after scrolling?

Any suggestions?


Solution

  • Good Question, this is your answer holder.setIsRecyclable(false).

    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
    
        LayoutInflater inflater = (LayoutInflater) parent.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = inflater.inflate(R.layout.product_recycle_buyer_list_item, parent, false);
        MyViewHolder holder = new MyViewHolder(view);
    
        holder.setIsRecyclable(false);
    
        return holder;
    }