Skip to content

Instantly share code, notes, and snippets.

@sudo5in5k
Created July 8, 2020 06:40
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 sudo5in5k/6d3cbe61213a8953c8bfbb5ced609c59 to your computer and use it in GitHub Desktop.
Save sudo5in5k/6d3cbe61213a8953c8bfbb5ced609c59 to your computer and use it in GitHub Desktop.
WrapHeightViewPager
/**
* wrap_contentを対応できるようにしたViewPager
*/
class WrapHeightViewPager @JvmOverloads constructor(
context: Context,
attrs: AttributeSet? = null
) : ViewPager(context, attrs) {
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
var localHeightMeasureSpec = heightMeasureSpec
var height = 0
children.forEach {
it.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
val h = it.measuredHeight
if (h > height) height = h
}
if (height != 0) {
localHeightMeasureSpec = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
}
super.onMeasure(widthMeasureSpec, localHeightMeasureSpec)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment