ViewPage自适应高度。

import android.content.Context
import android.support.v4.view.ViewPager
import android.support.v4.view.ViewPagerUtils
import android.util.AttributeSet

class ViewPage2(context: Context, attrs: AttributeSet?) : ViewPager(context, attrs) {
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {

        var heightSpec = heightMeasureSpec
        val currentView = ViewPagerUtils.getCurrentView(this)
        if (currentView!=null) {
            currentView.forceLayout()
            currentView.measure(widthMeasureSpec, MeasureSpec.makeMeasureSpec(MeasureSpec.getSize(heightMeasureSpec), MeasureSpec.UNSPECIFIED))
            heightSpec = MeasureSpec.makeMeasureSpec(currentView.measuredHeight, MeasureSpec.EXACTLY)
        }
        super.onMeasure(widthMeasureSpec, heightSpec)
    }
}

你可能感兴趣的:(ViewPage自适应高度。)