Search code examples
androidleanback

How to write a custom Leanbacks VerticalGridView in Android TV?


I want to implement a Row from the Details screen of the Leanback library into a customized screen. The row will be the one below. I have already implemented the HorizontalGridView and have managed to get the items to be shown.

Sample Image

My layout:

<android.support.v17.leanback.widget.HorizontalGridView
android:id="@+id/detail_related"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>

My Java:

RecyclerView.Adapter mAdapter = new SimilarAssetsAdapter();
mBinding.detailRelated.setAdapter(mAdapter);

The problem I am encountering is that I cannot focus on any of the items but the focus is on the whole HorizontalGridView. How do I solve this?


Solution

  • To solve this, use a VerticalGridView instead of a HorizontalGridView.

    <android.support.v17.leanback.widget.VerticalGridView
        android:id="@+id/detail_related"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>
    

    To set the ArrayObjectAdapter to the VerticalGridView, use an ItemBridgeAdapter.

    ItemBridgeAdapter adapter = new ItemBridgeAdapter();
    adapter.setAdapter(mRowsAdapter);
    adapter.setPresenter(new SinglePresenterSelector(new ListRowPresenter()));
    mBinding.detailRelated.setAdapter(adapter);