Skip to content

Instantly share code, notes, and snippets.

@dadouf
Created June 2, 2020 08:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dadouf/c75dfd4f9228af8b6e687caa6240c241 to your computer and use it in GitHub Desktop.
Save dadouf/c75dfd4f9228af8b6e687caa6240c241 to your computer and use it in GitHub Desktop.
ViewPager that wraps its content's height
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