Skip to content

Instantly share code, notes, and snippets.

@nikhilpanju
Last active December 6, 2019 16:51
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save nikhilpanju/8d5a91ef0e124f5792a2104e61a54a87 to your computer and use it in GitHub Desktop.
Save nikhilpanju/8d5a91ef0e124f5792a2104e61a54a87 to your computer and use it in GitHub Desktop.
Gist to showcase expanding/collpasing items
override fun onBindViewHolder(holder: ListViewHolder, position: Int) {
val model = adapterList[position]
// set expand state on binded items because views are recycler
expandItem(holder, model == expandedModel, animate = false)
holder.cardContainer.setOnClickListener {
if (expandedModel == null) {
// expand clicked view
expandItem(holder, expand = true, animate = true)
expandedModel = model
} else if (expandedModel == model) {
// collapse clicked view
expandItem(holder, expand = false, animate = true)
expandedModel = null
} else {
// collapse previously expanded view
val expandedModelPosition = adapterList.indexOf(expandedModel!!)
val oldViewHolder =
recyclerView.findViewHolderForAdapterPosition(expandedModelPosition) as? ListViewHolder
if (oldViewHolder != null) expandItem(oldViewHolder, expand = false, animate = true)
// expand clicked view
expandItem(holder, expand = true, animate = true)
expandedModel = model
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment