this is the code for expanding/collapsing an item. how can I Expand all items in a recycler view with a single button click?
private fun expandParentRow(position: Int){ \\to expand an item
val currentBoardingRow = list[position]
val services = currentBoardingRow.gameDetes
currentBoardingRow.isExpanded = true
var nextPosition = position
if(currentBoardingRow.type == Constants.PARENT) {
services?.forEach { service ->
val parentModel = IndividualReport()
parentModel.type = Constants.CHILD
val subList: ArrayList<GameDete> = ArrayList()
subList.add(service)
parentModel.gameDetes = subList
list.add( nextPosition, parentModel)
}
notifyDataSetChanged()
}
}
private fun collapseParentRow(position: Int){ \\ to collapse an item
val currentBoardingRow = list[position]
val services = currentBoardingRow.gameDetes
list[position].isExpanded = false
if(list[position].type==Constants.PARENT){
services?.forEach { _ ->
list.removeAt(position 1)
}
notifyDataSetChanged()
}
}
CodePudding user response:
Just simply do a forEach or a for loop to iterate all items of the recycle view, set the .isExpanded to true and finally call notifyDataSetChanged().
For example:
fun expandAllItems() {
val currentList = list.map { it.copy() }
currentList.forEachIndexed { index, item ->
item.isExpanded = true
val services = item.gameDetes
if(item.type == Constants.PARENT) {
services?.forEach { service ->
val parentModel = IndividualReport()
parentModel.type = Constants.CHILD
val subList: ArrayList<GameDete> = ArrayList()
subList.add(service)
parentModel.gameDetes = subList
list.add( index, parentModel)
}
}
}
notifyDataSetChanged()
}
This should do the job.
CodePudding user response:
you can expand with animation (new animate() api) with this code block:
private fun RecyclerView.showAnimation(
duration: Long,
startFrom: Int = 0,
itemsDelay: Long = 50,
fromAlpha: Float = 1.0f,
toAlpha: Float = 1.0f,
fromScaleY: Float = 1.0f,
toScaleY: Float = 1.0f,
onAnimationStart: () -> Unit = {},
onAnimationEnd: () -> Unit = {}
) {
viewTreeObserver.addOnPreDrawListener(
object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
viewTreeObserver.removeOnPreDrawListener(this)
for (i in startFrom until childCount) {
val v: View = getChildAt(i)
v.alpha = fromAlpha
v.scaleY = fromScaleY
v.pivotY = 0f
v.animate().apply {
scaleY(toScaleY)
alpha(toAlpha)
setDuration(duration)
startDelay = (i * itemsDelay)
withStartAction(onAnimationStart)
withEndAction(onAnimationEnd)
start()
}
}
return true
}
})
}