Skip to content

Multiple View Types

Yogesh Choudhary Paliyal edited this page Oct 12, 2021 · 2 revisions

Example 1

Show multiple view types in your listing, for eg: heading and list
implement UniversalViewType in your model and override getLayoutId()

screenshot : view fullscreen

example :

Heading Model

data class HeadingModel(val title: String) : UniversalViewType,SchoolListing {
    override fun getLayoutId(): Int {
        return R.layout.item_heading
    }
}

List Model

data class ListItemModel(val name: String) : UniversalViewType,SchoolListing {
    override fun getLayoutId(): Int {
        return R.layout.item_list
    }
}

SchoolListing.kt

This interface is used to identify models to use in a recycler view, to prevent adding of other type of models in array except for models that implements SchoolListing

interface SchoolListing

Adding data to array

val tempArray = ArrayList<SchoolListing>()
        tempArray.add(HeadingModel("Principal"))
        tempArray.add(ListItemModel("Yogesh Paliyal"))

        tempArray.add(HeadingModel("Staff"))
        tempArray.add(ListItemModel("Sachin Rupani"))
        tempArray.add(ListItemModel("Suraj Vaishnav"))
        tempArray.add(ListItemModel("Himanshu Choudhan"))
        tempArray.add(ListItemModel("Pramod Patel"))
        tempArray.add(ListItemModel("Bharath"))
        tempArray.add(ListItemModel("Sanjay"))
        tempArray.add(ListItemModel("Surendra Singh"))


        tempArray.add(HeadingModel("Students"))
        tempArray.add(ListItemModel("Bhoma Ram"))
        tempArray.add(ListItemModel("Deepak"))
        tempArray.add(ListItemModel("Sohan"))
        tempArray.add(ListItemModel("Umesh"))
        tempArray.add(ListItemModel("Amanda Howard"))
        tempArray.add(ListItemModel("Jeremy Glover"))
        tempArray.add(ListItemModel("Ginger Larson"))
        tempArray.add(ListItemModel("Lincoln Pierpoint"))
        tempArray.add(ListItemModel("Brian Brooks"))
        tempArray.add(ListItemModel("Erasmus Hall"))
        tempArray.add(ListItemModel("Amber Lane"))
        tempArray.add(ListItemModel("Elsie Cole"))

View full example

Example 2

Show multiple, here 3 view types in your listing, for eg: message sent, message received and date header
implement UniversalViewType in your model and override getLayoutId()

screenshot : view fullscreen

example:

Heading Model

data class HeadingModel(val title: String) : UniversalViewType, ChatListing {
    override fun getLayoutId(): Int {
        return R.layout.item_chat_header
    }
}

List Model

data class ListItems(val name: String,val time:String,val type:String) : UniversalViewType, ChatListing {
    override fun getLayoutId(): Int {
        if(type == "MessageSent"){
            return R.layout.item_row_message_sent
        }
        else{
            return R.layout.item_row_message_received
        }
    }
}

ChatListing.kt

This interface is used to identify models to use in a recycler view, to prevent adding of other type of models in array except for models that implements ChatListing

interface ChatListing

Adding data to array

        val tempArray = ArrayList<ChatListing>()

        tempArray.add(HeadingModel("Yesterday"))
        tempArray.add(ListItems("Wassup","1:40 PM","MessageSent"))
        tempArray.add(ListItems("Just preparing for exams","1:43 PM","MessageReceived"))
        tempArray.add(ListItems("Great!","1:47 PM","MessageSent"))

        tempArray.add(HeadingModel("Today"))
        tempArray.add(ListItems("Hello","12:25 AM","MessageSent"))
        tempArray.add(ListItems("I am fine","12:27 AM","MessageReceived"))
        tempArray.add(ListItems("What about you","12:27 AM","MessageReceived"))
        tempArray.add(ListItems("I am great!","12:30 AM","MessageSent"))

View full example