Updated on 2026-08-14

This commit is contained in:
Tangem 2020-12-11 16:59:23 +03:00
parent b9429d2710
commit 60a332149e
4 changed files with 55 additions and 6 deletions

View file

@ -0,0 +1,31 @@
package com.tangem.tap.common
import android.view.View
import android.view.ViewTreeObserver
/**
[REDACTED_AUTHOR]
*/
class GlobalLayoutStateHandler<T: View>(
private val view: T,
attachImmediately: Boolean = true
) : ViewTreeObserver.OnGlobalLayoutListener {
var onStateChanged: ((T) -> Unit)? = null
init {
if (attachImmediately) attach()
}
fun attach() {
view.viewTreeObserver.addOnGlobalLayoutListener(this)
}
fun detach() {
view.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
override fun onGlobalLayout() {
onStateChanged?.invoke(view)
}
}

View file

@ -3,9 +3,13 @@ package com.tangem.tap.common.extensions
import android.view.LayoutInflater
import android.view.ViewGroup
import android.view.ViewParent
import androidx.core.view.forEach
import androidx.transition.AutoTransition
import androidx.transition.Transition
import androidx.transition.TransitionManager
import com.google.android.material.chip.Chip
import com.google.android.material.chip.ChipGroup
import com.tangem.tap.common.GlobalLayoutStateHandler
import timber.log.Timber
/**
@ -25,4 +29,19 @@ fun ViewParent?.beginDelayedTransition(transition: Transition = AutoTransition()
fun ViewGroup.beginDelayedTransition(transition: Transition = AutoTransition()) {
TransitionManager.beginDelayedTransition(this, transition)
}
fun ChipGroup.fitChipsByGroupWidth() {
val layoutStateHandler = GlobalLayoutStateHandler(this)
layoutStateHandler.onStateChanged = stateHandler@{
if (it.childCount < 2) {
layoutStateHandler.detach()
return@stateHandler
}
val spacingBetweenViews = it.chipSpacingHorizontal * (it.childCount - 1)
val width = (it.width - spacingBetweenViews) / it.childCount
it.forEach { chip -> (chip as? Chip)?.width = width }
layoutStateHandler.detach()
}
}