Skip to content Skip to sidebar Skip to footer

Android Tv. Adding Custom Views To Verticalgridfragment

I am building an Android TV app and want to add some views to VerticalGridFragment. Is that possible and how? I am using leanback library version 25.0.0. Thank you for all the answ

Solution 1:

The leanback-showcase example provided by the leanback team has a great example on how to do this. I highly recommend you clone that repo and play around in that project.

You'll basically want to initialize an Adapter with a Presenter that knows how to present the views you'd like to display in your list. This class right here has a specific example of exactly what you're looking for.

In the example they use a PresenterSelector, but if your list is homogenous (backed by only one model), then you can pass in a single Presenter directly into the Adapter - like the Presenterhere.

In code - first setup your grid presenter

VerticalGridPresentergridPresenter=newVerticalGridPresenter(ZOOM_FACTOR);
gridPresenter.setNumberOfColumns(COLUMNS);
setGridPresenter(gridPresenter);

Then set your adapter on the VerticalGridFragment

PresenterSelectorcardPresenterSelector=newCardPresenterSelector(getActivity());
mAdapter = newArrayObjectAdapter(cardPresenterSelector);
setAdapter(mAdapter);

Then add models to your Adapter

privatevoidcreateRows() {
    String json = Utils.inputStreamToString(getResources()
            .openRawResource(R.raw.grid_example));
    CardRow row = new Gson().fromJson(json, CardRow.class);
    mAdapter.addAll(0, row.getCards());
}

Post a Comment for "Android Tv. Adding Custom Views To Verticalgridfragment"