Multiple ListRows for each Header on BrowseFragment - Leanback library

后端 未结 2 1775
Happy的楠姐
Happy的楠姐 2021-02-14 10:53

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

2条回答
  •  孤独总比滥情好
    2021-02-14 11:27

    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.

提交回复
热议问题