如何在RowsFragment中始终显示标题

时间:2016-02-02 11:05:23

标签: android android-fragments leanback

我正在尝试创建类似于Youtube应用程序的Android tv应用程序。我正在使用Sofa库来做到这一点。现在,当我试图在RowsFragment中显示单个项目的标题时,问题就出现了,即使RowsFragment没有焦点。以下是将数据集加载到browseFragment

的代码
private void loadRowsCustom() {
        adapter = new ArrayObjectAdapter();

        int split = 3;
        int rowsFragmentCount = videoList.size() / split + (videoList.size() % split != 0 ? 1 : 0);

        for (int i = 0; i < rowsFragmentCount; i++) {

            ListRowPresenter lrp = new ListRowPresenter(FocusHighlight.ZOOM_FACTOR_LARGE);
            ArrayObjectAdapter rowsAdapter = new ArrayObjectAdapter(lrp);

            String category = "Category";

            for (int j = 0; j < split && (i * split + j) < videoList.size(); j++) {
                Video[] videos = videoList.get(i * split + j);
                ArrayObjectAdapter rowCollAdapter = new ArrayObjectAdapter(new VideoPresenter());
                rowCollAdapter.addAll(0, Arrays.asList(videos));
                ListRow row = new ListRow(new HeaderItem(videos[0].category),rowCollAdapter);
                rowsAdapter.add(row);
                category = videos[0].category;
            }

            ArrayObjectAdapter fragmentAdapter = new ArrayObjectAdapter();
            RowsSupportFragment rowsSupportFragment = new RowsSupportFragment();
            rowsSupportFragment.setAdapter(rowsAdapter);
            rowsSupportFragment.enableRowScaling(false);
            fragmentAdapter.add(rowsSupportFragment);
            adapter.add(new ListRow(new HeaderItem(category), fragmentAdapter));
        }

        browseSupportFragment.setAdapter(adapter);
        browseSupportFragment.setHeadersState(BrowseSupportFragment.HEADERS_ENABLED);
        browseSupportFragment.setOnSearchClickedListener(searchClickListener);
        browseSupportFragment.setOnItemViewClickedListener(browseClickListener);
        browseSupportFragment.setTitle("Google Videos");
    }

下面的屏幕截图中描述了所需的和当前的实现:

enter image description here

enter image description here

2 个答案:

答案 0 :(得分:4)

正如Mayur Kaul已经提到过的,你只需要打电话

mRowsFragment.setExpand(true)

但在这种情况下,每次调用onAttachedToWindow(...)方法时(请参阅android.support.v17.leanback.app.RowsFragment - ItemBridgeAdapter.AdapterListener mBridgeAdapterListener),您的标题标题都会被隐藏。

所以最终解决方案,巫婆为我工作它只是简单地覆盖你的RowsFragment实现中的setExpand()方法,如下所示:

@Override
    public void setExpand(boolean expand) {
        super.setExpand(true);
    }

答案 1 :(得分:1)

对于那些面临类似问题的人,我终于设法找到一个可以产生预期效果的黑客。但为此您可能需要获取BrowseSupportFragmentSofa的来源并修改以下代码行。我知道它有点破解,但它得到了理想的输出。

对于BrowseFragment:Notice the mRowsFragment.setExpand(true)

private void showHeaders(boolean show) {
        if (DEBUG) Log.v(TAG, "showHeaders " + show);
        mHeadersFragment.setHeadersEnabled(show);
        setHeadersOnScreen(show);
        setRowsAlignedLeft(!show);
        if (mRowsFragment != null) {
            mRowsFragment.setExpand(true);
        } else if (mCurrentFragment != null && mCurrentFragment instanceof RowsFragment) {
            ((RowsFragment) mCurrentFragment).setExpand(true);
        }
    }

同样适用于BrowseSupportFragment

private void showHeaders(boolean show) {
        if (DEBUG) Log.v(TAG, "showHeaders " + show);
        mHeadersSupportFragment.setHeadersEnabled(show);
        setHeadersOnScreen(show);
        setRowsAlignedLeft(!show);
        if (mRowsSupportFragment != null) {
            mRowsSupportFragment.setExpand(true);
        } else if (mCurrentFragment != null && mCurrentFragment instanceof RowsSupportFragment) {
            ((RowsSupportFragment) mCurrentFragment).setExpand(true);
        }
    }