Search code examples
androidgridviewandroid-gridviewandroid-tvleanback

How to replace list of channels to grid list?


I have a TV app. I see list of channels and I made it be like a row. But now I want to rewrite it to be like a grid. I'm trying to set grid presenter, but it doesn't work it says I must implement MainFragmentAdapterProvider. And all tips from tutorials doesn't work too, I can't figure out what's a problem. Maybe there's another presenter for it?

    public class ChannelsFragment extends RowsFragment implements OnItemViewClickedListener {

    private ArrayObjectAdapter mRowsAdapter = new ArrayObjectAdapter(new ListRowPresenter());
    //private ArrayObjectAdapter mRowsAdapter;
    private IServiceControl mServiceControl;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mServiceControl = ((A4TVTestApp) getActivity().getApplication()).getServiceControl();

        setAdapter(mRowsAdapter);
        setOnItemViewClickedListener(this);

        createRows();
    }

    private void createRows() {
//        VerticalGridPresenter presenter = new VerticalGridPresenter();
//        presenter.setNumberOfColumns(4);
//        setGridPresenter(presenter);
//        setTitle(getString(R.string.scanned_channels));
//
//        setOnItemViewClickedListener(this);
//
//        try {
//            for (int i = 0; i < mServiceControl.getServiceListCount(0); i++) {
//                mRowsAdapter.add(mServiceControl.getServiceDescriptor(0, i).getName());
//            }
//        } catch (RemoteException e) {
//            e.printStackTrace();
//        }
        ChannelsRowItemPresenter presenter = new ChannelsRowItemPresenter();

        ArrayObjectAdapter firstRowAdapter = new ArrayObjectAdapter(presenter);
        ArrayObjectAdapter secondRowAdapter = new ArrayObjectAdapter(presenter);
        ArrayObjectAdapter thirdRowAdapter = new ArrayObjectAdapter(presenter);
        ArrayObjectAdapter fourthRowAdapter = new ArrayObjectAdapter(presenter);

        try {
            int count = mServiceControl.getServiceListCount(0);
            for (int i = 0; i < count; i += 4) {
                firstRowAdapter.add(mServiceControl.getServiceDescriptor(0, i));
                secondRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 1));
                thirdRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 2));
                fourthRowAdapter.add(mServiceControl.getServiceDescriptor(0, i + 3));
            }
        } catch (RemoteException e) {
            e.printStackTrace();
        }

        mRowsAdapter.add(new ListRow(new HeaderItem(getString(R.string.scanned_channels)),
                firstRowAdapter));

        mRowsAdapter.add(new ListRow(secondRowAdapter));
        mRowsAdapter.add(new ListRow(thirdRowAdapter));
        mRowsAdapter.add(new ListRow(fourthRowAdapter));
    }

    @Override
    public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                              RowPresenter.ViewHolder rowViewHolder, Row row) {
        ServiceDescriptor serviceDescriptor = (ServiceDescriptor) item;
        ChannelActivity.startActivity(getActivity(), serviceDescriptor.getLCN());
    }
}

Solution

  • Have you tried extending VerticalGridFragment?

    There is an example in the leanback sample: https://github.com/googlesamples/androidtv-Leanback/blob/master/app/src/main/java/com/example/android/tvleanback/ui/VerticalGridFragment.java

    Your implementation should look similar to this below. MyPresenter should be a subclass of Presenter, such as CardPresenter in leanback which delegates/manages a view like ImageCardView or your custom card view by extending BaseCardView. If you have more dynamic content, subclass PresenterSelector instead so that each Card can be different.

    public class ChannelFragment extends android.support.v17.leanback.app.VerticalGridFragment
    implements OnItemViewClickedListener {
    
        private static final int NUM_COLUMNS = 5;
    
        private final ArrayObjectAdapter mRowsAdapter;
        private IServiceControl mServiceControl;
    
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
    
            mRowsAdapter = new ArrayObjectAdapter(new MyPresenter());
            mServiceControl = ((A4TVTestApp) getActivity().getApplication()).getServiceControl();
            setAdapter(mRowsAdapter);
            setOnItemViewClickedListener(this);
    
            createRows();
        }
    
        private void createRows() {
            VerticalGridPresenter gridPresenter = new VerticalGridPresenter();
            gridPresenter.setNumberOfColumns(NUM_COLUMNS);
            // This is the method that will help you get a grid layout.
            setGridPresenter(gridPresenter);
    
            setOnItemViewClickedListener(this);
    
            try {
                for (int i = 0; i < mServiceControl.getServiceListCount(0); i++) {
                    mRowsAdapter.add(i);
                }
            } catch (RemoteException e) {
                 e.printStackTrace();
            }
    
        }
    
    
        @Override
        public void onItemClicked(Presenter.ViewHolder itemViewHolder, Object item,
                          RowPresenter.ViewHolder rowViewHolder, Row row) {
             ChannelActivity.startActivity(getActivity(), (int) item);
        }
    }