How to set HeaderItem on the left side using VerticalGridView like Android TV Home Screen
Asked Answered
G

0

1

I am very new to Leanback Libraries. I want to develop Android TV home screen, where the CustomHeaderItem will be on left side and for each HeaderItem there will be one ListRow and instead of BrowseFragment, i want to render whole things on VerticalGridView. Like below picture.

enter image description here

To achiveve this, i tried to keep an ImageView with HorizontalGridView and both views i rendered inside VerticalGridView.

While doing so, the focus is moving from imageView to respective HorizontalGridView on clicking on Right dpad Key which is expected behaviour but coming back from respective HorizontalGridView to respective ImageView is not happening.

row_grid_view_renderer.xml:

<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:lb="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
    android:id="@+id/header_icon"
    android:layout_width="300dp"
    android:paddingBottom="50dp"
    android:layout_height="150dp"
    android:layout_gravity="center"
    android:gravity="center"
    android:focusable="true"
    android:layout_weight="1"
    android:clickable="true"
    android:background="@drawable/selector_bg_card"
    android:focusableInTouchMode="true" />
<androidx.leanback.widget.HorizontalGridView
  android:id="@+id/row_grid_view"
  android:layout_width="match_parent"
  android:layout_height="wrap_content"
  android:focusable="true"
  android:layout_gravity="center"
  android:focusableInTouchMode="true"
  android:layout_weight="2"
  lb:horizontalMargin="12dip"
  lb:verticalMargin="24dip"
  lb:numberOfRows="3"
  lb:rowHeight="150dip" />
</LinearLayout>

main_grid_view_rendere.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="wrap_content"
android:layout_height="wrap_content">
<androidx.leanback.widget.VerticalGridView
    android:id="@+id/parent_grid_view"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" />
</LinearLayout>

Below i am overriding the createRowViewHolder() method of ListRowPresenter class to set the imageView and HorizontalGridView.

public class CustomListRowPresenter extends ListRowPresenter {
private static final String TAG = CustomListRowPresenter.class.getSimpleName();
private static final boolean DEBUG = true;
private CustomListRowView rowView;

public CustomListRowPresenter() {
    super();
}

public RowPresenter.ViewHolder createRowViewHolder(ViewGroup parent) {
    rowView = new CustomListRowView(parent.getContext(),null,-1);
    rowView.getImageView().setImageResource(R.drawable.ic_blur_on_black_24dp);
    return new ViewHolder(rowView,  rowView.getRowGridView() , this);
}

And here is my MainActivity class which is rendering ImageView and HorizontalGridView inside main_grid_view(which is holding VerticalGridView).

 @Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_grid_view);
    mVerticalGridView = (VerticalGridView) findViewById(R.id.parent_grid_view);
    loadRows();
}
private void loadRows() {
    cardPresenter = new CardPresenter();
    rowsAdapter = new ArrayObjectAdapter(new CustomListRowPresenter());
    List<Movie> list = MovieList.setupMovies();

    int i;
    for (i = 0; i < NUM_ROWS; i++) {
        if (i != 0) {
            Collections.shuffle(list);
        }
        ArrayObjectAdapter listRowAdapter = new ArrayObjectAdapter(cardPresenter);
        for (int j = 0; j < NUM_COLS; j++) {
            listRowAdapter.add(list.get(j % 5));
        }
        HeaderItem header = new HeaderItem(i, MovieList.MOVIE_CATEGORY[i]);
        rowsAdapter.add(new CustomListRow(header, listRowAdapter));
    }
    ItemBridgeAdapter itemBridgeAdapter = new ItemBridgeAdapter(rowsAdapter);
    mVerticalGridView.setAdapter(itemBridgeAdapter);
}

I want to get the focus in both respective View while pressing Right and Left key of Dpad. How i will achieve the above scenario or where i am going wrong ? And also, How to stop header transition inside BrowseFragment ?

Thanks in advance :)

Gingrich answered 1/4, 2020 at 4:45 Comment(3)
Can you show some of your code so we can see what you're doing currently?Ettieettinger
@MichaelCeley, sorry for the late reply.. above i have mentioned the code for reference.Gingrich
Have you got the any way to achieve thisEctophyte

© 2022 - 2024 — McMap. All rights reserved.