4

I have a list of elements in a recycler view that are generated dynamically using data binding. Each element has a bottom view that is initially set to View.GONE, however once a user clicks the element and the view is expanded, the recycler view automatically scrolls back to the top of the list. Conversely, if the view is expanded and then clicked again to collapse, the recycler view scrolls to the top of the list again. I have tried keeping track of the ID's of the elements for the adapter (again using data binding), setting focus to the child element when they expand or collapse, and using binding adapters to animate the expand/collapse itself.

My suspicion is that the adapter in the recycler view is receiving a onNotifyDataChanged() alert when the height of one of the child changes, rendering an additional view (still inside one of the children though, not a separate child in the list), and automatically scrolls to the top. Is there a way to override this? Does anyone know what else might be causing the list to snap to the top when one of the elements is clicked -> expanded/collapsed?

Nick Borisenko
  • 494
  • 3
  • 18
  • How you are managing view to show after clicking on element? Do you call anything like notify...? – Pankaj Kumar Jun 28 '18 at 16:04
  • Currently I just have a data binding that toggles the expandable view from View.VISIBLE <-> View.GONE once the view is clicked. So something like `android:visibility="@{viewModel.expand.value ? View.VISIBLE : View.GONE}"` – Nick Borisenko Jun 28 '18 at 16:07

1 Answers1

2

So I found the solution to my issue in case someone encounters something similar. I actually ended up finding this answer: RecyclerView notifyDataSetChanged scrolls to top position

and the second solution involving the staggered layout manager was the route I went with, since I want to be able to wrap the content. My suspicions were correct that it had to do with the changing height of the elements in the recycler view. The expandable item's parent wrapped content, so the recycler view was forced to recalculate the size of the item once the expandable portion appeared/disappeared (at least that's what I got out of it).

Nick Borisenko
  • 494
  • 3
  • 18