Last active
January 29, 2020 01:56
-
-
Save satoshun/94a2f416e8bdfa57f6ab87ad96d5b3ef 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
import android.content.Context | |
import android.util.AttributeSet | |
import androidx.constraintlayout.widget.ConstraintHelper | |
import androidx.constraintlayout.widget.ConstraintLayout | |
import androidx.core.view.isGone | |
import androidx.core.view.marginLeft | |
import androidx.core.view.marginRight | |
import androidx.core.view.updateLayoutParams | |
class GoneOverflow @JvmOverloads constructor( | |
context: Context, | |
attrs: AttributeSet? = null, | |
defStyleAttr: Int = 0 | |
) : ConstraintHelper(context, attrs, defStyleAttr) { | |
override fun updatePostMeasure(container: ConstraintLayout) { | |
super.updatePostMeasure(container) | |
val views = getViews(container) | |
if (container.measuredWidth == 0) return | |
if (views.size <= 1) return | |
val target = views.getOrNull(0) ?: return | |
if (target.isGone) return | |
val totalWidth = views.map { it.marginLeft + it.measuredWidth + it.marginRight }.sum() | |
if (container.measuredWidth < totalWidth) { | |
target.updateLayoutParams { width = 0 } | |
target.isGone = true | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment