Multiple ListRows for each Header on BrowseFragment - Leanback library
Asked Answered
B

2

10

I'm getting started with Leanback support for our app. As per UI requirements I need to add multiple list rows corresponding to each header, it's exactly like what Youtube App does on Android TV. Default ListRowPresenter seems to be rendering only one list row and its header. Is there any presenter that supports multiple list rows? I'm thinking on the lines creating a custom presenter with RowsFragment embedded in each item, correct me if my approach is wrong.enter image description here

Bixby answered 3/7, 2015 at 8:25 Comment(1)
Just a note - you might want to check out my answer below. The newest version of Leanback solves this problem: https://mcmap.net/q/1046235/-multiple-listrows-for-each-header-on-browsefragment-leanback-libraryJeremiah
J
20

The Leanback team has recently added in support for multiple ListRows for one HeaderItem in version 24.0.0 of the library. It allows you to supply a RowsFragment that maps to the HeaderItem. You can see an example of it in their Leanback showcase. Specifically, here is the file where they provide an example.

There is a new PageRowFragmentFactory that you will need in your BrowseFragment which specifies which Fragments map to which HeaderItems. Like so:

    @Override
    public Fragment createFragment(Object rowObj) {
        Row row = (Row)rowObj;
        mBackgroundManager.setDrawable(null);
        if (row.getHeaderItem().getId() == HEADER_ID_1) {
            return new SampleFragmentA();
        } else if (row.getHeaderItem().getId() == HEADER_ID_4) {
            return new WebViewFragment();
        }
        throw new IllegalArgumentException(String.format("Invalid row %s", rowObj));
    }

You can just have the above method return an instance of a RowsFragment and now you'll have the RowsFragment which contains multiple ListRows map to just one HeaderItem.

As of right now you can get access to this goodness through version 24.0.0 of Leanback with the below line in your gradle file:

compile 'com.android.support:leanback-v17:24.0.0

You might get a warning, but for now it can be safely ignored.

There is also a ton of other really cool stuff in version 24.0.0 of Leanback like snazzy transition animations and cleaner APIs. It can all be found in that sample project I linked above. There is also a talk from Google I/O which covers more of the additions.

Jeremiah answered 13/6, 2016 at 23:39 Comment(3)
@Kyle Venn : I did the same as that sample project above but search button not auto hide. ( Image: i.imgur.com/mCeCj0o.png ) Can you help me ? Can you help me ?Coatbridge
@MrSiro, I'm happy to take a look - but I'll be more helpful if I can see your code. Can you open up a new Stack Overflow question with your code and provide me with the link? The hiding and showing happens through the BrowseFragment talking to the TitleView. And that should all work automatically.Jeremiah
@KyleVenn : I solved the problem. Thank you. Can you help our the error at: #48577244Coatbridge
F
7

Is there any presenter that supports multiple list rows?

Not that I know of. The problem is that BrowseFragment only accepts children that subclass Row. For this reason, there can only be single rows (and their respective headers) for each entry in BrowseFragment.

I'm thinking on the lines creating a custom presenter with RowsFragment embedded in each item, correct me if my approach is wrong.

As I just mentioned, I doubt that creating a custom presenter would help.

The only solution I have been able to come up with so far is simply creating a custom version of BrowseFragment (by manually extending the RowsFragment and HeadersFragment classes) so that it supports any type of fragments.

If you're interested, I wrote a series of articles on the process https://medium.com/building-for-android-tv/

and a base project that offers the custom version of BrowseFragment https://github.com/dextorer/BuildingForAndroidTV

I'm also considering writing a library to ease the use of this custom component.

Fulgurous answered 3/7, 2015 at 8:46 Comment(2)
Perfect, you pin pointed the solution of my problem. Loved all your Android TV articles, saved me tons of hours of source code inspection.Bixby
Dextor, Any lead to create Custom Details Fragment with different UI (Two Header Item) would be really helpful.Sclerotic

© 2022 - 2024 — McMap. All rights reserved.