Created
June 2, 2020 08:45
-
-
Save dadouf/c75dfd4f9228af8b6e687caa6240c241 to your computer and use it in GitHub Desktop.
ViewPager that wraps its content's height
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.support.v4.view.ViewPager | |
import android.util.AttributeSet | |
internal class HeightWrappingViewPager @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null) | |
: ViewPager(context, attrs) { | |
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) { | |
var heightMeasureSpecModif = heightMeasureSpec | |
var height = 0 | |
for (i in 0 until childCount) { | |
val child = getChildAt(i) | |
child.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)) | |
val h = child.measuredHeight | |
if (h > height) height = h | |
} | |
if (height != 0) { | |
heightMeasureSpecModif = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY) | |
} | |
super.onMeasure(widthMeasureSpec, heightMeasureSpecModif) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment