Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ViewPager2 - How to use registerOnPageChangeCallback to check view pager item position

How can I use ViewPager2's registerOnPageChangeCallback facility to check the position of the currently visible ViewPAger2 fragment? I know this will consists of an if/when statement but don't know the correct way to implememnt this.

mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (mViewPager.currentPage) == 0 {
                // do something
                }
                else if (mViewPager.currentPage) == 1 {
                // do something
                }
                else {
                // do something
                }
                super.onPageSelected(position)
            }
        })
like image 687
wbk727 Avatar asked Apr 25 '20 18:04

wbk727


People also ask

What's new in viewpager2?

ViewPager2 uses RecyclerView, one of the most used widgets in the Android world, to show collections of items. This is a huge change for the ViewPager API, as RecyclerView is a powerful and robust widget. It supports vertical paging by using LinearLayoutManager.

How do I add a viewpager to an existing layout?

Steps for implementing viewpager: Adding the ViewPager widget to the XML layout (usually the main_layout). Creating an Adapter by extending the FragmentPagerAdapter or FragmentStatePagerAdapter class. An adapter populates the pages inside the Viewpager.

How to access the recyclerview inside a viewpager2?

to access the internal RecyclerView used inside the ViewPager2 for now, and you can call findViewHolderForAdapterPosition () or findViewHolderForItemId () as usual. I had a custom view implementing a ViewPager which could, in some cases, modify the inner views without having to recompute all of them for a small change.

What is onpageselected and onpagescrollstatechanged in doppelgangerviewpager?

onPageSelected (int position): When a new page is selected, ViewPager 2 invokes this method. onPageScrollStateChanged (@ScrollState int state): ViewPager 2 invokes this when the scroll state of the current page changes. Now you need to wire your doppelgangerViewPager with the doppelgangerPageChangeCallback so it can start to get the callbacks.


1 Answers

Use the passed position to know where you are:

 mViewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
            override fun onPageSelected(position: Int) {
                if (position == 0) {
                // you are on the first page
                }
                else if (position == 1) {
                // you are on the second page
                }
                else if (position == 2){
                // you are on the third page
                }
                super.onPageSelected(position)
            }
        })
like image 111
Hasan Bou Taam Avatar answered Sep 17 '22 14:09

Hasan Bou Taam