I'm trying to programatically scroll to an item in my RecyclerView
based on position.
The following code works when scrolling to an item in the RecyclerView
adapters data if the position is visible on screen / rendered:
recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
However if I set a position to a value which is not yet visible on the screen / rendered, the page is only scrolled just below the last item which is visible / already rendered. E.g. it works for the first 4 items which are visible, but not the 4 which are off screen.
For extra details the data in the adapter is set in an onSuccess
callback:
// update the data in the adapter after hitting a REST API
adapter.updateItems(data);
adapter.notifyDataSetChanged();
// then I scroll to the position
recyclerView.getLayoutManager()).scrollToPositionWithOffset(position, 0);
Other methods to invoke the scrolling haven't worked - e.g. just calling recyclerView.scrollToPosition(position)
And the view is rendered inside a BottomSheetDialog
.
There seem to be quite a few questions relating to similar things which have not solved my problems - eg RecyclerView - How to smooth scroll to top of item on a certain position?
UPDATE
Actually trying it with a different longer data set its not only the fact that the item position is not visible - it appears to only be the last items which don't scroll correctly (e.g the last three).
Thanks!