Search code examples
androidandroid-tvleanbackbrowsefragment

Navigate between PageRow elements with image view


I want to add an image to header elements. I followed this tutorial and added images successfully. It is using ListRow element and works perfect with ListRow.

However, when I change ListRow elements with PageRow, it doesn't navigate between headers. Here are the some critical changes. What could be the reason?

IconHeaderItemPresenter.java

@Override
public void onBindViewHolder(Presenter.ViewHolder viewHolder, Object o) {
    IconHeaderItem iconHeaderItem = (IconHeaderItem)((PageRow)o).getHeaderItem();
    View rootView = viewHolder.view;
...
}

MainFragment.java

public void createRows(){

    IconHeaderItem headerItem1 = new IconHeaderItem(1, getString(R.string.header_history), R.drawable.header_images);
    PageRow pageRow1 = new PageRow(headerItem1);
    mRowsAdapter.add(pageRow1);

    IconHeaderItem headerItem2 = new IconHeaderItem(2, getString(R.string.header_recommendations), R.drawable.header_musics);
    PageRow pageRow2 = new PageRow(headerItem2);
    mRowsAdapter.add(pageRow2);

    IconHeaderItem headerItem3 = new IconHeaderItem(3, getString(R.string.header_news), R.drawable.header_videos);
    PageRow pageRow3 = new PageRow(headerItem3);
    mRowsAdapter.add(pageRow3);

    IconHeaderItem headerItem4 = new IconHeaderItem(4, getString(R.string.header_notifications), R.drawable.header_settings);
    PageRow pageRow4 = new PageRow(headerItem4);
    mRowsAdapter.add(pageRow4);
}

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

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

    @Override
    public Fragment createFragment(Object rowObj) {
        Row row = (Row)rowObj;
        mBackgroundManager.setDrawable(null);
        //id for news fragment
        if (row.getHeaderItem().getId() == 3) {
            return new SampleFragmentA();
        } else {
            return new SampleFragmentB();
        }

    }
}

Solution

  • add these two lines

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

    to the onCreateViewHolder of your extended RowHeaderPresenter