yuyakaido / CardStackView

📱Tinder like swipeable card view for Android
Apache License 2.0
2.36k stars 448 forks source link

How can I use different layout (xml) for every card? #309

Open RasyiidWho opened 4 years ago

Barryrowe commented 4 years ago

This will be expensive to destroy and inflate fully new layouts for every item, but can be done.

The card stack is a recyclerview, and you can extend ListAdapter<X, Y> for your CardStackAdapter implementation to handle this. You will need to determine the layout type based on position, and then build the appropriate ViewHolder:

override fun getItemViewType(position: Int): Int {
     return  someCustomLogicToDeterminLayoutResourceByPosition(position)
}

Then in onCreateViewHolder you'll need to build the appropriate ViewHolder for the layout type. This will likely require a base class for your ViewHolders, and custom ViewHolders per type of item.

I would recommend finding a way to NOT have unique layouts per card if you have a large number of cards in your stack, as it will be expensive. But it is possible.