为带 PageRow 的 Leanback BrowseFragment 自定义 Headers

Custom Headers for leanback BrowseFragment with PageRow

该代码由一个 BrowseFragment 组成,它有多个列表行与每个 header 行关联,参考此代码,我能够使用 PageRow 而不是 ListRow 正确实现它 (https://github.com/googlesamples/leanback-showcase/blob/master/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/page/PageAndListRowFragment.java)

问题出在帮助我自定义 headers 的函数(向 headers 添加图标:

browseFragment.setHeaderPresenterSelector(new PresenterSelector() {
        @Override
        public IconHeaderPresenter getPresenter(Object item) {
            return new IconHeaderPresenter();
        }

    });

添加后,header 不再被选中,我无法在它们之间导航。

这就是我创建浏览片段的方式:

    ...     
    browseFragment = new BrowseFragment();
    browseFragment.setHeadersState(BrowseFragment.HEADERS_ENABLED);
    browseFragment.prepareEntranceTransition();
    browseFragment.setHeaderPresenterSelector(new PresenterSelector() {
        @Override
        public IconHeaderPresenter getPresenter(Object item) {
            return new IconHeaderPresenter();
        }

    });

    mBackgroundManager = BackgroundManager.getInstance(shared.main);
    mBackgroundManager.attach(shared.main.getWindow());
    browseFragment.getMainFragmentRegistry().registerFragment(IconPageRow.class,
            new PageRowFragmentFactory(mBackgroundManager));

...

private static class PageRowFragmentFactory extends BrowseFragment.FragmentFactory {
    private final BackgroundManager mBackgroundManager;

    PageRowFragmentFactory(BackgroundManager backgroundManager) {
        this.mBackgroundManager = backgroundManager;
    }

    @Override
    public Fragment createFragment(Object rowObj) {
        ArrayObjectAdapter adapter=((IconPageRow)rowObj).getAdapter();
        RowsFragment rowsFragment = new RowsFragment();
        rowsFragment.enableRowScaling(true);
        rowsFragment.setAdapter(adapter);
        mBackgroundManager.setDrawable(null);
        rowsFragment.setOnItemViewClickedListener(browseClickListener);
        rowsFragment.setOnItemViewSelectedListener(browseSelectedListener);
        return rowsFragment;
    }
}

解决方案是在 onCreateViewHolder 中向我的自定义 Header 演示者 (IconHeaderPresenter) 添加以下两行:

view.setFocusable(true);
view.setFocusableInTouchMode(true);

在 IconHeaderPresenter() 中。

View view = inflater.inflate(R.layout.icon_header_item, null);

icon_header_item.xml如下:

<android.support.v17.leanback.widget.ImageCardView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:backgroundTint="@color/fastlane_background"
    android:clickable="true"
    android:paddingBottom="20dp"
    android:contextClickable="true"
    android:orientation="vertical">
//your layout here
</android.support.v17.leanback.widget.ImageCardView>