1

I am creating a scrollable list of text views, for which I have chosen to use the RecyclerView. I would like to modify the behaviour of the RecyclerView to allow scrolling of the first and last items to the centre of the screen. Please review this gif of the current behaviour, where I am able to verically scroll until "0" or "9" are visible:

enter image description here

What I would like to achieve is being able to scroll until the first item has reached the centre, not when it becomes visible at the top. The same applies to the last item, where I would like to scroll until it has reached the centre, not when it becomes visible at the bottom.

Poliziano
  • 569
  • 4
  • 14

1 Answers1

4

sophin's suggestion helped me solve my problem. The answer was fairly straightforward: inherit from LinearLayoutManager and override the padding methods. In my case, I needed to override getPaddingTop and getPaddingBottom, because I was scrolling vertically:

class CustomLinearLayoutManager(context: Context): LinearLayoutManager(context) {
    override fun getPaddingTop(): Int {
        return (height / 2) - 100
    }

    override fun getPaddingBottom(): Int {
        return paddingTop
    }
}

Here I calculate the padding by dividing the height of the LinearLayoutManager by 2. I subtract 100 because that is half of the height of the children. I expect this calculation can be modified to work for children of varing heights.

recycler view scrolling from top to bottom

Poliziano
  • 569
  • 4
  • 14