android-viewpager2
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
ViewPager2 default position
I think an easier more reliable fix is to defer to next run cycle instead of unsecure delay e.g viewPager.post { viewPager.setCurrentItem(1, true) }
Viewpager2 with fragments and Jetpack navigation: Restore fragments instead of recreating them
You can have initial pages of ViewPager as NavHostFragment which have their own back stacks which will result having the implementation in gif below Create a NavHost fragment for each tab or can have generalized one will add it either /** * Using [FragmentStateAdapter.registerFragmentTransactionCallback] with [FragmentStateAdapter] solves back navigation instead of using [OnBackPressedCallback.handleOnBackPressed] in every … Read more
Pages must fill the whole ViewPager2 (use match_parent)
I have found out that the problem is in inflating view holder. I had the same issue and solved it changing ViewBinding.inflate(inflater) to ViewBinding.inflate(inflater, parent, false)
How to use TabLayout with ViewPager2 in Android
You have to use this TabLayoutMediator that mimics tabLayout.setupWithViewPager() and sets up the ViewPager2 with Tablayout. Otherwise, you will have to write your own adapter that will combine both parties. Its code will look like this in Kotlin TabLayoutMediator(tabLayout, viewPager) { tab, position -> tab.text = tabTitles[position] }.attach()
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
How to disable swiping in ViewPager2?
Now it is possible to enable-disable swiping viewpager2 using Version 1.0.0-alpha02 Use implementation ‘androidx.viewpager2:viewpager2:1.0.0-alpha02’ Version 1.0.0 New features Ability to disable user input (setUserInputEnabled, isUserInputEnabled) API changes ViewPager2 class final Bug fixes FragmentStateAdapter stability fixes SAMPLE CODE to disable swiping in viewpager2 myViewPager2.setUserInputEnabled(false); SAMPLE CODE to enable swiping in viewpager2 myViewPager2.setUserInputEnabled(true);
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