nadine87
nadine87

Reputation: 530

Custom Headers for leanback BrowseFragment with PageRow

The code consists of a BrowseFragment that has multiple list rows associated to each header row, in reference to this code, i was able to implement it correctly using PageRow instead of ListRow (https://github.com/googlesamples/leanback-showcase/blob/master/app/src/main/java/android/support/v17/leanback/supportleanbackshowcase/app/page/PageAndListRowFragment.java)

The problem is with this function that helps me customize the headers (add icon to the headers:

enter image description here

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

    });

Once I add it, the headers are no longer selected, and I can't navigate between them.

This is how I am creating the browsefragment:

    ...     
    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;
    }
}

Upvotes: 3

Views: 3852

Answers (2)

Sid
Sid

Reputation: 21

In IconHeaderPresenter().

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

icon_header_item.xml is as below:

<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>

Upvotes: 2

nadine87
nadine87

Reputation: 530

The solution to this was to add to my Custom Header presenter (IconHeaderPresenter) in onCreateViewHolder these two lines:

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

Upvotes: 4

Related Questions