androidkotlinfastadapter

FastAdapter Combine Headers and items


I usage library fastadapter by mikepenz https://github.com/mikepenz/FastAdapter

Need combine diferents views

Header 1
Item A
Item B
Item C
Hader 2
Item D
Item E
....

Class for item

open class SimpleItemVB : AbstractBindingItem<DrawSimpleItemListBinding>() {
    var name: String? = null

    override val type: Int
        get() = R.id.fastadapter_icon_item_id

    override fun bindView(binding: DrawSimpleItemListBinding, payloads: List<Any>) {
        binding.tvTitle.text = name
    }

    override fun createBinding(
        inflater: LayoutInflater,
        parent: ViewGroup?
    ): DrawSimpleItemListBinding {
        return DrawSimpleItemListBinding.inflate(inflater, parent, false)
    }
}

Class for display Headers

open class HeaderVB(
        val title: String? = null
) : AbstractBindingItem<DrawHeaderItemListBinding>() {


    override val type: Int
        get() = R.id.fastadapter_header_item_id

    override fun bindView(binding: DrawHeaderItemListBinding, payloads: List<Any>) {
        binding.tvTitle.text = title
    }

    override fun createBinding(
            inflater: LayoutInflater,
            parent: ViewGroup?
    ): DrawHeaderItemListBinding {
        return DrawHeaderItemListBinding.inflate(inflater, parent, false)
    }
}

For generate items

    private fun fetchItems(): ArrayList<SimpleItemVB> {
        val items = ArrayList<SimpleItemVB>()

        //How to add header???
        for (i in 1..100) {
            
            //How to add header every 3 items???
            val simpleItem = SimpleItemVB()
            simpleItem.name = "Test $i"
            simpleItem.identifier = (100 + i).toLong()
            items.add(simpleItem)
        }
        return items
    }

I have no idea how to assemble the adapter so that it can have two different types of view.

A simple example? since what is in the repository is too complicated to understand


Solution

  • The FastAdapter library builds around the concept of providing a type safe interface to construct adapters for your RecyclerView. This type safety is also deeply nested in its API (via the Generic type specification for example) to ensure no type conflicts occur.

    Usually you'd have only the same types within a list, or items which share a common parent type. If the items are different enough, they will still always share the IItem<RecyclerView.ViewHolder type.

    The library exposes for these scenarios a type alias to allow defining your adapter as GenericFastAdapter, and similar to that also expose a GenericItemAdapter or GenericModelAdapter depending on your usecase.

    Beyond that the library also provides a GenericFastItemAdapter (combining FastAdapter and ItemAdapter)

    The sample app of the FastAdapter includes a sample showcasing the usage of such:

    https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/IconGridActivity.kt

    Similar there's another example showcasing a similar usecase with different models which are mapped to their respective items here:

    https://github.com/mikepenz/FastAdapter/blob/develop/app/src/main/java/com/mikepenz/fastadapter/app/MultiTypeModelItemActivity.kt