Is it possible to have a ViewPager inside of a ScrollView?

后端 未结 5 943
失恋的感觉
失恋的感觉 2020-11-29 01:22

I\'m trying to use a ViewPager inside of a ScrollView, but the ViewPager does not appear. If I remove the ScrollView the

5条回答
  •  野趣味
    野趣味 (楼主)
    2020-11-29 01:50

    In case someone is having the same problem, when I use the class below it works fine.

    class CustomWrapContentViewPager(cont: Context, attr: AttributeSet?) : ViewPager(cont,attr) {
    
    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        var height = 0
        for (i in 0 until childCount) {
            val child: View = getChildAt(i)
            child.measure(
                widthMeasureSpec,
                MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED)
            )
            val h: Int = child.measuredHeight
            if (h > height) height = h
        }
        val heightMeasure = MeasureSpec.makeMeasureSpec(
            height,
            MeasureSpec.EXACTLY
        )
        super.onMeasure(widthMeasureSpec, heightMeasure)
        }
    }
    

提交回复
热议问题