Created
July 8, 2020 06:40
-
-
Save sudo5in5k/6d3cbe61213a8953c8bfbb5ced609c59 to your computer and use it in GitHub Desktop.
WrapHeightViewPager
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
/** | |
* 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