Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android TV Leanback RowsFragment keep focused item at the start

I'm working on a Leanback app and I've implemented the rows and everything via RowsFragment and its all working great.

As for now when I go left and right within the items in the row the focused item moves to the middle of the screen and gains the focus.

Example -

enter image description here

I want the focused item to stay at the beginning of the row and not at the middle.

Example -

enter image description here

Couldn't find anything on this on the web. Would really appreciate any information regarding this.

like image 464
Itay Feldman Avatar asked Nov 28 '18 11:11

Itay Feldman


3 Answers

If you also want the last item of the row to appear at the beginning, use this ListRowPresenter:

class FocusedItemAtStartListRowPresenter(focusZoomFactor: Int) : ListRowPresenter(focusZoomFactor) {
    override fun createRowViewHolder(parent: ViewGroup): RowPresenter.ViewHolder {
        val viewHolder = super.createRowViewHolder(parent)

        with((viewHolder.view as ListRowView).gridView) {
            windowAlignment = BaseGridView.WINDOW_ALIGN_LOW_EDGE
            windowAlignmentOffsetPercent = 0f
            windowAlignmentOffset = parent.resources.getDimensionPixelSize(R.dimen.lb_browse_padding_start)
            itemAlignmentOffsetPercent = 0f
        }

        return viewHolder
    }
}

This is similar to what the Leanback Launcher does.

like image 163
user3210008 Avatar answered Sep 22 '22 08:09

user3210008


Faced the same issue and solved it by using a custom ListRowPresenter:

override fun createRowViewHolder(parent: ViewGroup): RowPresenter.ViewHolder {
        val viewHolder = super.createRowViewHolder(parent)

        val itemWidth = parent.resources.getDimensionPixelSize(R.dimen.epg_program_width)

        with((viewHolder as ListRowPresenter.ViewHolder).gridView) {
            itemAlignmentOffset = parent.width / 2 - itemWidth / 2 - paddingLeft
        }

        return viewHolder
    }
like image 21
Fredrik Eneroth Avatar answered Sep 23 '22 08:09

Fredrik Eneroth


To be complete as tvlauncher you need also set this:

fadingLeftEdge = true // to fade previous item out

But better is to use their FadingEdgeContainer, as fading edge is not really the same.

Something like this: https://github.com/bosphere/Android-FadingEdgeLayout

like image 35
Milan Jurkulak Avatar answered Sep 20 '22 08:09

Milan Jurkulak