Android Leanback : how to keep selected row centered vertically on the screen

131 Views Asked by At

I have a DetailsSupportFragment() screen in which I've implemented 4 rows. When I scroll between the rows, it always stays on top of the screen. I would like the selected row to be centered vertically on the screen (even the last row).

I've implemented this but the last row is not scrolling smoothly (a bit jumpy).

 rowsSupportFragment.verticalGridView.setOnChildSelectedListener { _, _, position, _ ->
            val childCount = rowsAdapter.size()
            
        when (position) {
            0 -> {
                rowsSupportFragment.verticalGridView.windowAlignment = WINDOW_ALIGN_HIGH_EDGE
                rowsSupportFragment.verticalGridView.windowAlignmentOffsetPercent =
                    WINDOW_ALIGN_OFFSET_PERCENT_DISABLED
            }
            in 1 until childCount - 1 -> {
                rowsSupportFragment.verticalGridView.windowAlignment = WINDOW_ALIGN_BOTH_EDGE
                rowsSupportFragment.verticalGridView.windowAlignmentOffsetPercent =
                    WINDOW_ALIGN_OFFSET_PERCENT_DISABLED
            }
            childCount - 1 -> {
                rowsSupportFragment.verticalGridView.windowAlignment = WINDOW_ALIGN_NO_EDGE
                rowsSupportFragment.verticalGridView.windowAlignmentOffsetPercent =
                    20f
            }
        }
    }`

Is there a better way to achieve the desired result in a more clean way? Thanks in advance !

M A

0

There are 0 best solutions below