mikepenz/FastAdapter

Multiple view types

Closed this issue · 4 comments

Hi @mikepenz,

Thank you for this amazing library! It has really sped up development for me and with all the features, there's so much I can do with a lot less code!

One thing I'm struggling with is handling different view types. I followed the examples (specifically the MultiTypeModelItemActivity.kt example) and it's working, however, I'm unsure if I'm doing it correctly.

I want something like:

  • Header
  • ViewType A
  • Header
  • ViewType A
  • ViewType B
  • Header
  • ViewType A

This is the code I have:

private lateinit var fastAdapter: GenericFastAdapter
//...
val itemAdapter = ModelAdapter { element: Any->
    when (element) {
        is HeaderSection-> HeaderSectionItem(element)
        is ViewTypeASection-> ViewTypeASectionItem(element)
        else -> {
            ViewTypeBSection(element)
        }
    }
}
//...
fastAdapter = FastAdapter.with(listOf(itemAdapter))
//...
itemAdapter.add(HeaderSection("Header"))
itemAdapter.add(ViewTypeASection("ATest1"))
itemAdapter.add(HeaderSection("Header"))
itemAdapter.add(ViewTypeASection("ATest2"))
itemAdapter.add(ViewTypeBSection("BTest1"))
itemAdapter.add(HeaderSection("Header"))
itemAdapter.add(ViewTypeASection("ATest3"))

Is this the correct way of approaching this? The reason I am asking this is because in the other examples (such as AdvancedSampleActivity.kt) multiple adapters have been created: a headerAdapter and itemAdapter. Does this mean I should create an adapter for each of them and pass them to fastadapter?

// Like this?
fastAdapter = FastAdapter.with(listOf(headerAdapter1, viewTypeAdapterA1, headerAdapter2, viewTypeAdapterA2)) //etc

Hopefully you can put me in the right direction! Thank you!

Checklist

@missphyrgames the answer to this is actually more of a "it depends".

Ultimately it depends on the exact usecase you want to achieve. If you have no real need to modify items in a section often, and if things stay stable then using a single adapter is for sure fine.

Using a different adapter per section will shine especially if you want to remove all items in a section, add items in a section without wanting to modify other elements.

Also it may be relevant also for type safety. as you can have a child adapter with only a specific item type.

So based on the very short example I would say stay with one adapter. As it won't scale if you have 10 adapters, ...

But if for example you have 3 sections. and those 3 sections then have a few hundreds items within. then it may make more sense to setup multiple adapters

Firstly, thank you for the quick reply!

Secondly, I found a concrete example of what I want to achieve. It's practically the same as the soundcloud library page. The elements of the page don't change or update at all. It's a completely static list.

2021-2-12_11-32-16

Would you then recommend the going the single adapter route if no items change?

I would say so. there's no major reason for you to split it up into a huge amount of sub adapters. the adapter you have will just allow for all the different types and you put them in.

In any case, if you see you want to go more complex or want to take benefit of being able to clear out a single adapter or so at a later point, replacing it should be rather simple and not cause any much complications.

That's great! Thanks again!