How to write custom Leanbacks VerticalGridView on Android TV?

I want to implement a Row on the Leanback Library Details screen on a custom screen. The line will be next. I have already implemented HorizontalGridView and I managed to get the elements that will 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 facing is that I cannot focus on any of the elements, but the focus is on the objectives of the HorizontalGridView . How to solve this?

+1
source share
5 answers

To solve this problem, use VerticalGridView instead of HorizontalGridView .

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

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

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

If your view is customizable, be sure to add the following to your custom view to get focus:

 setFocusable(true); setFocusableInTouchMode(true); 
0
source

The problem is in the user view - you need to add a focus change listener to the java code. Add a focus listener inside the adapter to the root view of the adapter elements.

Add custom for rootview to oncreate adapter elements.

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

Then in onBind add focuschangeListener.

  yourRootView.setOnFocusChangeListener(new View.OnFocusChangeListener() { @Override public void onFocusChange(View v, boolean hasFocus) { if(hasFocus){ //focus gained }else { //focus lost } } }); 
0
source

I know little about the Android application for Android, I can not say anything looking at how you managed to present ListRows, but I feel that you can find a solution in after the link .

0
source

@sharath kumar

Thanks so much for solving my problem. I struggled with navigation using D-PAD in my Google TV app. when the presentation is reworked, the focus is lost. The reason was that the focus moved from the button on the list item to the next item / root view item.

Using your approach, I was able to concentrate the child button request whenever the rootview receives focus.

Thanks again!

0
source

Source: https://habr.com/ru/post/1241026/


All Articles