Search code examples
androidandroid-tvleanback

Adding Multiple Presenters to ArrayObjectAdapter of Android Leanback


I want to display two different types of cards in the same row (ListRow).

I tried to modify the Presenter class by adding viewType similar to dealing with displaying multiple row types in RecyclerVew but it did not work.

I tried digging into the Presenter and ArrayObjectAdapter code but there is no obvious option to achieve this. If someone has done this then please point me in the right direction.


Solution

  • You can use ClassPresenterSelector to show different types of cards in the same row

    Example

    ClassPresenterSelector selector = new ClassPresenterSelector();
    
    YourDetailTypePresenter detailPresenter = new YourDetailTypePresenter();
    YourImageTypePresenter imagePresenter = new YourImageTypePresenter();
    
    selector.addClassPresenter(DetailsOverviewRow.class, detailPresenter);
    selector.addClassPresenter(ImageOverviewRow.class, imagePresenter);
    
    mRowsAdapter = new ArrayObjectAdapter(selector);