Visitable Pattern in Recycler View

Ankit Kumar
1 min readJul 24, 2022

Inflating multiple views in recycler view

Since in normal approach we have all our logics in the adapter only which makes it quite difficult to add or manipulate viewHolders later on

So we will be looking on visitable pattern to achieve the same to make our adapter quite generic and make it extensible and easy to manipulate

Every model which needs to added in our adapter will be inheriting from BaseItemModel

ItemTypeFactory responsible for returning the layout for specific model class

For each model class we need a view holder so will be inheriting from AbstractViewHolder

private val adapter = ContactAdapter(ItemTypeFactoryImp(),listOf())

So as you know how to create adapter, so whenever we want to add a new type of item in recycler view we will

  • Inherit our model class with BaseItemModel
  • Inherit our ViewHolder from AbstractViewHolder
  • Add the new type in our ItemTypeFactory

Can refer to this github project

--

--