1

I'm trying to build an app with a RecycleView. Every element in the RecycleView has a complex layout with buttons that are changing colors and animations and stuff. After the user "done" with the current item and start scrolling, this item will be recycled and the layout changes I made will be lost.

When the user scrolls to that item again I'll have to change the layout again to way it was.

Is there an easy way to save an item's layout state (positions and sizes of the buttons, colors of views...) and then restore them when the view is recycled again?

Also is there a way to restore a layout to its original state? I would like to use it when I get a recycled view that has layout changes that I want to restore?

ben
  • 1,064
  • 3
  • 15
  • 29
  • Keep that info in a map: `SparseArray`. – azizbekian Aug 03 '17 at 11:16
  • it wouldn't work cause your reference will be recycle any way, you could use one of the tricks from this link : https://stackoverflow.com/questions/20721752/prevent-the-adapter-from-recycling-views-on-scroll – user1796260 Aug 03 '17 at 11:18

1 Answers1

0

I assume you have a list of elements using which you populate the RecyclerView adapter. You can save the states of the element inside that object. And during onBindViewHolder you can show a proper layout based on the states in the element object. Or you can even change the whole layout of the view using getItemViewType(position).

Another way to save the states of the elements is in a separate Map rather than in the Element object itself, like this: Map<ElementId, ElementState>

Bob
  • 13,447
  • 7
  • 35
  • 45