BrowseFragment上每个标题的多个ListRows-Leanback库 [英] Multiple ListRows for each Header on BrowseFragment - Leanback library

查看:271
本文介绍了BrowseFragment上每个标题的多个ListRows-Leanback库的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在开始为我们的应用提供Leanback支持. 根据UI的要求,我需要添加与每个标题对应的多个列表行,这就像Youtube App在Android TV上所做的一样.默认的 ListRowPresenter 似乎仅呈现一个列表行及其标题.是否有任何演示者支持多个列表行?我正在考虑创建在每个项目中嵌入RowsFragment的自定义演示者的方法,如果我的方法有误,请纠正我.

解决方案

Leanback团队最近在库版本24.0.0中为一个HeaderItem添加了对多个ListRow的支持.它允许您提供映射到HeaderItemRowsFragment.您可以在其 Leanback展示柜中查看其示例.具体来说,此处是他们提供示例的文件.

BrowseFragment中将需要一个新的PageRowFragmentFactory,它指定哪个片段映射到哪个HeaderItem.像这样:

    @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));
    }

您只需使上述方法返回RowsFragment的实例,现在您将拥有RowsFragment,其中包含多个ListRow映射到一个HeaderItem.

截至目前,您可以通过Leanback的24.0.0版访问此功能,并且在gradle文件中使用以下行:

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

您可能会收到警告,但现在可以放心地忽略它.

版本24.0.0的Leanback中还有很多其他非常酷的东西,例如时髦的过渡动画和更清洁的API.所有这些都可以在我上面链接的示例项目中找到.还有来自Google I/O的对话,其中涵盖了更多的内容.

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.

解决方案

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.

这篇关于BrowseFragment上每个标题的多个ListRows-Leanback库的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆