3
votes

I am using the Leanback library and I would like to know how to create multiple custom Row Views. For creating different items in a row you need to extend PresenterSelector

I tried doing the same for the ListRowPresenter but couldn't achieve the right result. No row was binded in the RowsSupportFragment and in the logs the getPresenter method from PresenterSelector was called multiple times until out of memory.

1

1 Answers

4
votes

For solving this I had to check the leanback showcase repository

Based on the class ShadowRowPresenterSelector I managed to find how to create a selector for my custom RowPresenters.

class ShadowRowPresenterSelector : PresenterSelector() {

    private val aCustomListRowPresenter by lazy { ACustomListRowPresenter() }
    private val bCustomListRowPresenter by lazy { BCustomListRowPresenter() }

    override fun getPresenter(item: Any): Presenter {
        return when (item) {
            is ARowVM -> {
                aCustomListRowPresenter
            }
            is BRowVM -> {
                bCustomListRowPresenter
            }
            else -> aCustomListRowPresenter
        }
    }

    override fun getPresenters(): Array<Presenter> {
        return arrayOf(aCustomListRowPresenter, bCustomListRowPresenter)
    }
}

What caused the method getPresenter to be called multiple times for me was that there I by mistake created every time a new object for my custom row presenter.

I hope this will help someone in the future.