2

Building my first Android TV application and got kinda confused.

I'm using androidTV-leanback library.

The root problem: I've two functions. One is for getting back to previous fragment (using popBackStackImmediate();) and other one for creatingLayout();. Both work, but the layout is made alot faster than I return to MainFragment (where I can see navigation drawer) which makes a BIG DELAY.

My solution (in thoughts): I could simply call creatingLayout(); when I get the focus of specific navigation drawer header item.

Question: Is it possible to get a header item focus (items name/some kind of an id) - by function/insert your solution?

I want to know on every user movement active header items name/id. For instance - "Category 3".

https://i.stack.imgur.com/vlRir.png

Is this correct way of thinking about my problem? Maybe there's another approach I could use?

MaaAn13
  • 264
  • 5
  • 24
  • 54

3 Answers3

2

You can receive a callback when a HeadersFragment item (the nav menu item) gains focus by setting a listener inside the BrowseFragment:

getHeadersSupportFragment().setOnHeaderViewSelectedListener(
    new HeadersSupportFragment.OnHeaderViewSelectedListener() {
        @Override
        public void onHeaderSelected(RowHeaderPresenter.ViewHolder viewHolder, Row row) {
            Toast.makeText(getActivity(), row.getHeaderItem().getName(), Toast.LENGTH_LONG).show();
        }
    }
);
dell116
  • 5,835
  • 9
  • 52
  • 70
  • 1
    This works. But strangely enough, setting this listener breaks the usual `BrowseSupportFragment` behavior (selecting the tiles to the right of the headers). To fix that one should use `BrowseSupportFragment#setSelectedPosition(mCategoryRowAdapter.indexOf(row))` – Roman Samoilenko Dec 31 '18 at 09:55
1

if i understand your question correctly that you want to know when a specific category selected by user. in this case you can check in selected listener that you set in main fragment like this:

private final class ItemViewSelectedListener implements OnItemViewSelectedListener {
    @Override
    public void onItemSelected(Presenter.ViewHolder itemViewHolder, Object item,
                               RowPresenter.ViewHolder rowViewHolder, Row row) {
        ListRow listRow = (ListRow)row;
        if(listRow.getId() == 4)
        {
            //do something
        }
    }
}

you can set id when add your listrows to BrowserFragment ArrayAdpator then check it like i said above.

hope it helps :)

destrat18
  • 197
  • 2
  • 14
0

This is how i finally got it working on the latest BrowseSupportFragment, place this inside onItemViewSelectedListener.


   getHeadersSupportFragment().setOnHeaderViewSelectedListener(
                (viewHolder, row1) -> {
                    if (viewHolder.view.hasFocus()){
                        //Do stuff here
                    }else{
                        //Do other stuff here
                    }
                    setSelectedPosition(mRowsAdapter.indexOf(row1));
                }
        );
Kev506
  • 1
  • 1