How to write a custom Leanbacks VerticalGridView in Android TV?
Asked Answered
N

4

4

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?

Nordin answered 29/11, 2017 at 13:55 Comment(0)
N
3

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);
Nordin answered 11/12, 2017 at 7:2 Comment(2)
can you please explain, what is mBinding in your code?Premillennial
mBinding is the DataBinding component for that particular layout where the above xml is placed.Manufacturer
R
0

If your view is a custom one, make sure you add the following to your customized view to get focus:

setFocusable(true);
setFocusableInTouchMode(true);
Recipient answered 6/12, 2017 at 6:52 Comment(0)
D
0

The problem in custom view is you need to add the focus change listener in the java code.Add the focus listener inside the adapter to the root view of the adapter items.

Add focusable for the rootview in the oncreate of the adapter items.

yourView.setFocusable(true);      
yourView.setFocusableInTouchMode(true);

Then in the onBind add the focuschangeListener.

 yourRootView.setOnFocusChangeListener(new View.OnFocusChangeListener() {
    @Override
    public void onFocusChange(View v, boolean hasFocus) {

        if(hasFocus){
           //focus gained
        }else {
          //focus lost
        }
    }
});
Distal answered 8/12, 2017 at 12:7 Comment(0)
M
0

Try to put android:descendantFocusability="afterDescendants" in your .xml file

<android.support.v17.leanback.widget.HorizontalGridView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:descendantFocusability="afterDescendants"/>
Moneyed answered 19/2, 2021 at 8:43 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.