Foggy day

Android(Kotlin) - Setting the viewpager2 height according to the height of the items 본문

Android

Android(Kotlin) - Setting the viewpager2 height according to the height of the items

jinhan38 2021. 4. 18. 20:57

 

 

	private fun setViewPager2Height(){
      viewPager2.setPageTransformer { page, position ->
                updatePagerHeightForChild(page, viewPager2)
            }
	}
    
    private fun updatePagerHeightForChild(view: View, pager: ViewPager2) {
        view.post {
            val wMeasureSpec = View.MeasureSpec.makeMeasureSpec(view.width, View.MeasureSpec.EXACTLY)
            val hMeasureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED)
            view.measure(wMeasureSpec, hMeasureSpec)
            pager.layoutParams = (pager.layoutParams).also { lp -> lp.height = view.measuredHeight }
            pager.invalidate()
        }
    }