lisawray/groupie

Create a recyclerview with multiple view types using Groupie

TaqiyEddine-B opened this issue · 4 comments

I want to create a recyclerview that contains two types of item view using Groupie. The first item view is the header of the list and the second item view for the rest items of the list. How can I achieve that using groupie?

This is the class that represents an item of my list. It implements the abstract class Item

override fun bind(viewHolder: GroupieViewHolder, position: Int) {

    viewHolder.tv_card_item_name.text = itemName
    viewHolder.tv_card_item_id_value.text = item.itemId.toString()
    viewHolder.tv_card_item_page_value.text = itemPage.toString()

}

override fun getLayout(): Int {
        return R.layout.card_item
}

override fun getViewType(): Int {
    return if (item.itemId == 0)
        ITEM_HEAD
    else
        ITEM_QUEUE
}

override fun createViewHolder(itemView: View): GroupieViewHolder {

    val view : View
    if (viewType == ITEM_HEAD)
        view = LayoutInflater.from(context).inflate(R.layout.card_item_first, parent, false)
     else
        view = LayoutInflater.from(context).inflate(R.layout.card_item, parent, false)

    return super.createViewHolder(view)
}

Hey! It's much easier than you think. Just make 2 items, one item for the header, and one item for the not-header, and Groupie will figure it out for you as you can just pass in a list containing both.

In the end it should look something like

groupieAdapter.updateAsync(listOf(HeaderItem()) + queueList.map { QueueItem(it) }))

or so.

The idea is that you can set up a list. I usually have a MutableList.() -> Unit method and use add(HeaderItem()) and addAll(queueList.map { QueueItem(it) }) and tends to look like this:

adapter.replaceItemsWith {
    add(HeaderItem())
    addAll(queueList.map { QueueItem(it) })
}

but I haven't added that extension to groupie as it's... well, overstepping boundaries imo.

Did that help?

Did that help?

Hey can you answer me how to add multiple view item using groupie like facebook

@investosena Hi, I wrote article where you can find example of creating different types of view https://mvaluyskiy.medium.com/creating-complex-feed-based-on-recyclerview-with-groupie-1909df9b381c