2020-04-25 00:15:37 +02:00
|
|
|
package eu.kanade.tachiyomi.widget
|
|
|
|
|
|
|
|
import android.content.Context
|
|
|
|
import android.util.AttributeSet
|
2020-07-31 16:29:32 +02:00
|
|
|
import androidx.core.view.forEach
|
2020-04-25 00:15:37 +02:00
|
|
|
import androidx.viewpager.widget.ViewPager
|
|
|
|
|
|
|
|
/**
|
|
|
|
* A [ViewPager] that sets its height to the maximum height of its children.
|
|
|
|
* This is a way to mimic WRAP_CONTENT for its height.
|
|
|
|
*/
|
|
|
|
class MaxHeightViewPager(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
|
|
|
|
|
|
|
|
override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
|
|
|
|
var measuredHeight = heightMeasureSpec
|
|
|
|
|
|
|
|
var height = 0
|
2020-07-31 16:29:32 +02:00
|
|
|
forEach {
|
|
|
|
it.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED))
|
|
|
|
val h = it.measuredHeight
|
2020-04-25 00:15:37 +02:00
|
|
|
if (h > height) height = h
|
|
|
|
}
|
|
|
|
if (height != 0) {
|
|
|
|
measuredHeight = MeasureSpec.makeMeasureSpec(height, MeasureSpec.EXACTLY)
|
|
|
|
}
|
|
|
|
|
|
|
|
super.onMeasure(widthMeasureSpec, measuredHeight)
|
|
|
|
}
|
|
|
|
}
|