Android recycler item placement

24 Views Asked by At

I'm trying to implement a list, where the main element is in the middle of the Recycler, while the previous and next are about 50% visible (50% off the Recycler view). Something similar to the picutre below

Expected layout

That would be the initial positioning when the recycler becomes visible, and the on scroll to previous or next, the view1 (previous) or view3 (next) would come to the centre and the view2 (current centre) would shift left or right

I've tried to use LinearLayoutManager especially the scrollToPositionWIthOffset, along with a guideline in the xml layout, to calculate the offset distance after the scroll to position. However, that becomes tricky, because although the view is reporting to be visible the view.getLocationOnScreen returns 0,0, as it is not yet present on the screen. I've tried the RecyclerView.viewTreeObserver.addOnGlobalLayoutListener.onGlobalLayout, however the view is still reporting the coordinates of 0,0 from getLocationOnScreen.

I'm completely unsure if I should try to position items via scrollToPositionWithOffset, as I can't get the view coordinates because it seems, I try to fetch them 'too early', meaning before the view is ready to do so, and even if I get the offset right it seems to get messy when moving from element to element in the recycler.

Not sure whether this would require a custom LayoutManager or the Android provided classes can handle what I'm trying to achieve either.

0

There are 0 best solutions below