Monday, 22 October 2018

AndroidTV - customizing ListRow for X amount of rows. Focus lost

I'm using Leanback library in my Android TV project. I wanted to customize ListRow so I can change the amount of content lines I want to show (default is 1).

I followed guide: https://corochann.com/browsefragment-listrow-customization-android-tv-application-hands-on-tutorial-18-705.html

What I came up with is:

class CustomListRowPresenter : ListRowPresenter() {

override fun onBindRowViewHolder(holder: RowPresenter.ViewHolder, item: Any) {
    /* This two line codes changes the number of rows of ListRow */
    val numRows = (item as CustomListRow).numRows
    (holder as ListRowPresenter.ViewHolder).gridView.setNumRows(numRows)

    super.onBindRowViewHolder(holder, item)
}

override fun initializeRowViewHolder(holder: RowPresenter.ViewHolder) {
    super.initializeRowViewHolder(holder)

    /* Disable Shadow */
    shadowEnabled = false
}

companion object {
    private val TAG = CustomListRowPresenter::class.java.simpleName
}

}




class CustomListRow : ListRow {
var numRows = 1

constructor(header: HeaderItem, adapter: ObjectAdapter) : super(header, adapter) {}

constructor(id: Long, header: HeaderItem, adapter: ObjectAdapter) : super(id, header, adapter) {}

constructor(adapter: ObjectAdapter) : super(adapter) {}

companion object {

    private val TAG = CustomListRow::class.java.simpleName
}

}

And:

override onCreate() {
    val adapter = SparseArrayObjectAdapter(CustomListRowPresenter())
    setAdapter(adapter)
}

private fun setContent(content: List<Data>) {
    content.forEachIndexed { index, data ->
        val position = index.toLong()
        val headerItem = HeaderItem(position, data.title)
        val presenterSelector = getMediaObjectPresenterSelector()
        val dataAdapter = ArrayObjectAdapter(presenterSelector)
        val listRow = CustomListRow(position, headerItem, dataAdapter)
        data.content.forEach { x ->
            dataAdapter.add(x)
        }
        listRow.numRows = 5
        dataAdapters.add(dataAdapter)
        adapter.set(position.toInt(), listRow)
    }
    refresh()
}

So, everything is fine. I get the rows I wanted, but there's a problem. For instance, if I want to show 2 rows, it works perfect. If I want to show more than 2 (so you don't see second customListRow in a view) it doesn't have focus. Basically, if I have 2+ rows and I try to move left to right to bottom to top, focus works perfect on first 2 rows only. What could cause this and how to fix it?



from AndroidTV - customizing ListRow for X amount of rows. Focus lost

No comments:

Post a Comment