3

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?

Dave Gee
  • 47
  • 1
  • 1
  • 10

6 Answers6

7

Doing this holder.setIsRecyclable(false); will transform your RecyclerView into a ListView

Instead do this

Just override this two methods inside your RecyclerAdapter

  @Override
    public int getItemViewType(int position) {
        return position;
    }

    @Override
    public long getItemId(int position) {
        return position;
    }
Gastón Saillén
  • 12,319
  • 5
  • 67
  • 77
5

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;
}
Gundu Bandgar
  • 2,593
  • 1
  • 17
  • 21
  • 1
    For anyone intended to use. Beware, this will transform your RecyclerView to ListeView. I mean your view recycling ability is gone. The whole point of RecyclerView is to have "recyclability" and you just destroyed all structure with `holder.setIsRecyclable(false)` :)) – Farid May 31 '19 at 07:44
5

setIsRecyclable(false) is bad solution, as it will create more and more views as you scroll, which ruins the whole point of using RecyclerView. Not only it takes more CPU, but the more you scroll and see new items, the more memory it will use. This is even worse, if you display bitmaps, as bitmaps tend to take huge amount of memory.

What you are supposed to do instead, is to implement onBindViewHolder to bind the view to the data that it's supposed to have. Also use cache in case of using bitmaps.

You can look at a sample code I've made here, which asks of a different problem I'd like to solve.

android developer
  • 114,585
  • 152
  • 739
  • 1,270
0

In case someone experiences this, holder.setIsRecyclable(false) will do it but then, it just makes the recycler view a list view, and also consumes more resources . Overriding the getItemViewType and getItemId should fix it.

sɐunıɔןɐqɐp
  • 3,332
  • 15
  • 36
  • 40
samstag
  • 1
  • 2
0

As they said above setIsRecyclable() is a bad choice and won't fix it. Override the 2 methods getItemId & getItemViewType and return position for both this will fix it.

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jun 28 '22 at 12:17
-1

If I understand the question correctly, you don't want to scroll above when you call set adapter. The best way I found is to call

YourRcv.swapAdapter(YourAdapter, Bolean removeAndRecycleExistingViews); // true if yes false if no

I found this method at line 1142 in package androidx.recyclerview.widget , class RecyclerView.java

EricSchaefer
  • 25,272
  • 21
  • 67
  • 103
mickdevil
  • 1
  • 1