Created
December 22, 2021 12:23
-
-
Save wishygupta/d5f6aba712b97eee6797700347cfc24c to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class DetailsAdapter : ExpandableAdapter<ExpandableAdapter.ViewHolder>() { | |
private val groupNames = listOf("Offer Details", "Properties", "Classes", "Payment Schedule") | |
private lateinit var offerDetail: OfferDetail | |
companion object { | |
const val VIEW_OFFER = 0 | |
const val VIEW_PROPERTY = 1 | |
const val VIEW_CLASS = 2 | |
const val VIEW_PAYMENT = 3 | |
} | |
fun setData(item: OfferDetail) { | |
this.offerDetail = item | |
notifyDataSetChanged() | |
} | |
override fun getChildItemViewType(groupPosition: Int, childPosition: Int): Int { | |
if (groupPosition == 0) | |
return super.getChildItemViewType(groupPosition, VIEW_OFFER) | |
if (groupPosition == 1) | |
return super.getChildItemViewType(groupPosition, VIEW_PROPERTY) | |
if (groupPosition == 2) | |
return super.getChildItemViewType(groupPosition, VIEW_CLASS) | |
if (groupPosition == 3) | |
return super.getChildItemViewType(groupPosition, VIEW_PAYMENT) | |
return super.getChildItemViewType(groupPosition, childPosition) | |
} | |
override fun getChildCount(groupPosition: Int): Int { | |
if (groupPosition == VIEW_OFFER) | |
return 1; | |
if (groupPosition == VIEW_PROPERTY) | |
return offerDetail.properties.size; | |
if (groupPosition == VIEW_CLASS) | |
return offerDetail.classes.size; | |
if (groupPosition == VIEW_PAYMENT) | |
return offerDetail.paymentSchedule.size; | |
return 0 | |
} | |
override fun getGroupCount(): Int { | |
return groupNames.size | |
} | |
override fun onBindChildViewHolder( | |
holder: ViewHolder, | |
groupPosition: Int, | |
childPosition: Int, | |
payloads: List<Any> | |
) { | |
if (holder.itemViewType == VIEW_PROPERTY) { | |
(holder as? ChildOfferPropertyVH)?.apply { | |
binding.name.text = offerDetail.properties[childPosition].name | |
} | |
} | |
if (holder.itemViewType == VIEW_CLASS) { | |
(holder as? ChildOfferClassVH)?.apply { | |
binding.classValue.text = offerDetail.classes[childPosition].name | |
} | |
} | |
if (holder.itemViewType == VIEW_PAYMENT) { | |
(holder as? ChildOfferPaymentVH)?.apply { | |
binding.name.text = offerDetail.paymentSchedule[childPosition].name | |
} | |
} | |
if (holder.itemViewType == VIEW_OFFER) { | |
(holder as? ChildOfferDetailVH)?.apply { | |
binding.offerNameValue.text = offerDetail.name | |
} | |
} | |
} | |
override fun onBindGroupViewHolder( | |
holder: ViewHolder, | |
groupPosition: Int, | |
expand: Boolean, | |
payloads: List<Any> | |
) { | |
(holder as? ParentVH)?.apply { | |
if (expand) | |
binding.expandImg.setImageResource(R.drawable.ic_collapse) | |
else | |
binding.expandImg.setImageResource(R.drawable.ic_expand) | |
binding.name.text = groupNames[groupPosition] | |
binding.expandImg.rotation = if (expand) 0f else -90.0f | |
} | |
} | |
override fun onCreateChildViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder { | |
if (viewType == VIEW_OFFER) | |
return LayoutInflater.from(viewGroup.context) | |
.let { ItemOfferDetailsBinding.inflate(it, viewGroup, false) } | |
.let(::ChildOfferDetailVH) | |
if (viewType == VIEW_PROPERTY) | |
return LayoutInflater.from(viewGroup.context) | |
.let { ItemOfferPropertyBinding.inflate(it, viewGroup, false) } | |
.let(::ChildOfferPropertyVH) | |
if (viewType == VIEW_CLASS) | |
return LayoutInflater.from(viewGroup.context) | |
.let { ItemOfferClassBinding.inflate(it, viewGroup, false) } | |
.let(::ChildOfferClassVH) | |
if (viewType == VIEW_PAYMENT) | |
return LayoutInflater.from(viewGroup.context) | |
.let { ItemOfferPaymentBinding.inflate(it, viewGroup, false) } | |
.let(::ChildOfferPaymentVH) | |
return LayoutInflater.from(viewGroup.context) | |
.let { ItemOfferDetailsBinding.inflate(it, viewGroup, false) } | |
.let(::ChildOfferDetailVH) | |
} | |
override fun onCreateGroupViewHolder(viewGroup: ViewGroup, viewType: Int): ViewHolder = | |
LayoutInflater.from(viewGroup.context) | |
.let { ItemEquityHeaderBinding.inflate(it, viewGroup, false) } | |
.let(::ParentVH) | |
override fun onGroupViewHolderExpandChange( | |
holder: ViewHolder, | |
groupPosition: Int, | |
animDuration: Long, | |
expand: Boolean | |
) { | |
holder as ParentVH | |
val arrowImage = holder.binding.expandImg | |
if (expand) { | |
ObjectAnimator.ofFloat(arrowImage, View.ROTATION, 0f) | |
.setDuration(animDuration) | |
.start() | |
} else { | |
ObjectAnimator.ofFloat(arrowImage, View.ROTATION, -90f) | |
.setDuration(animDuration) | |
.start() | |
} | |
} | |
} | |
private class ChildOfferDetailVH(val binding: ItemOfferDetailsBinding) : | |
ExpandableAdapter.ViewHolder(binding.root) | |
private class ChildOfferPropertyVH(val binding: ItemOfferPropertyBinding) : | |
ExpandableAdapter.ViewHolder(binding.root) | |
private class ChildOfferClassVH(val binding: ItemOfferClassBinding) : | |
ExpandableAdapter.ViewHolder(binding.root) | |
private class ChildOfferPaymentVH(val binding: ItemOfferPaymentBinding) : | |
ExpandableAdapter.ViewHolder(binding.root) | |
private class ParentVH(val binding: ItemEquityHeaderBinding) : | |
ExpandableAdapter.ViewHolder(binding.root) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment