Search code examples
androidandroid-recyclerviewandroid-roomandroid-architecture-componentsandroid-jetpack

Paging library - one recycler with multiple view types


I am using all Android architecture components in my project (Room DB, Live Data etc.) Currently, I am facing a problem that I have RecyclerView which should used loaded data from Room DB and display it with Paging library. The problem is that there is a multiple data classes which represents the items in newsfeed and are stored in Room and I need display them in that one recycler.

Is there any way how to easily solve it? Can I for example create some interface which would be used by all these classes?


Solution

  • You could create and interface

    public interface NeewsFeedItem
        String getTitle();
        int getType();
        String data();
    ...
    

    Each of your model implement NeewsFeedItem and inside your Adapter you decide what type of view to show and how to show the proper NeewsFeedItem.

    You could override getItemViewType to show different presentation for different NeewsFeed's types.

    Also you could check FlexibleAdapter library that could help to manage your adapter with different types, headers, footers etc.