ViewPager2 with differing item heights and WRAP_CONTENT

The solution is to register a PageChangeCallback and adjust the LayoutParams of the ViewPager2 after asking the child to re-measure itself. pager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() { override fun onPageSelected(position: Int) { super.onPageSelected(position) val view = // … get the view view.post { val wMeasureSpec = MeasureSpec.makeMeasureSpec(view.width, MeasureSpec.EXACTLY) val hMeasureSpec = MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED) view.measure(wMeasureSpec, hMeasureSpec) if (pager.layoutParams.height … Read more

Proper implementation of ViewPager2 in Android

UPDATE 7 Check : Migrate from ViewPager to ViewPager2 Check : Create swipe views with tabs using ViewPager2 UPDATE 6 Check out my answer if you want to implement Carousel using View Pager2 UPDATE 5 How to use TabLayout with ViewPager2 SAMPLE CODE Use below dependencies implementation ‘com.google.android.material:material:1.1.0-alpha08’ implementation ‘androidx.viewpager2:viewpager2:1.0.0-beta02’ SAMPLE CODE XMl layout <?xml … Read more

Why `PagerAdapter::notifyDataSetChanged` is not updating the View?

There are several ways to achieve this. The first option is easier, but bit more inefficient. Override getItemPosition in your PagerAdapter like this: public int getItemPosition(Object object) { return POSITION_NONE; } This way, when you call notifyDataSetChanged(), the view pager will remove all views and reload them all. As so the reload effect is obtained. … Read more