Login/Register
Determine when a ViewPager changes pages
Asked Answered
T

5

171

I have three pages (Fragments) inside a ViewPager, however I only want to display a menu item for two of those pages.

The code given in a previous SO answer does not seem to work:

@Override
public void setUserVisibleHint(boolean isVisibleToUser) {
    super.setUserVisibleHint(isVisibleToUser);

    if (isVisibleToUser == true) { }
    else if (isVisibleToUser == false) {  }

}

Eclipse says @Override isn't needed and super can't be set. It's never called by the system, and even if it was how would I determine which page was being shown at the moment? Could I have some help here?

Theretofore answered 2/7, 2012 at 12:2 Comment(1)
This one worked for me, and gives a little more context about how to use the given solution: Detect ViewPager tab change inside Fragment.Neumeyer
S
329

Use the ViewPager.onPageChangeListener:

viewPager.addOnPageChangeListener(new OnPageChangeListener() {
    public void onPageScrollStateChanged(int state) {}
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

    public void onPageSelected(int position) {
        // Check if this is the page you want.
    }
});
Symploce answered 2/7, 2012 at 13:16 Comment(1)
You can use a SimpleOnPageChangeListener instead and only override onPageSelectedBum
V
127

ViewPager.setOnPageChangeListener is deprecated now. You now need to use ViewPager.addOnPageChangeListener instead.

for example,

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        }

        @Override
        public void onPageSelected(int position) {

        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });
Violette answered 16/6, 2015 at 12:32 Comment(3)
I seem not to understand. Am an absolute beginner. So what do we put inside those methods. Please help me on where to learn addOnPageChangeListener.Truckage
+1 for the info, but what is the difference of these two ViewPager.SimpleOnPageChangeListener and ViewPager.OnPageChangeListener as what mention above?Proboscis
@RoCk With SimpleOnPageChangeListener you only need to override the methods you actually want to use. See https://mcmap.net/q/142932/-determine-when-a-viewpager-changes-pages for example.Ramp
R
46

For ViewPager2,

viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
  override fun onPageSelected(position: Int) {
    super.onPageSelected(position)
  }
})

where OnPageChangeCallback is a static class with three methods:

onPageScrolled(int position, float positionOffset, @Px int positionOffsetPixels),
onPageSelected(int position), 
onPageScrollStateChanged(@ScrollState int state)
Rahn answered 22/9, 2019 at 2:56 Comment(0)
P
40

You can also use ViewPager.SimpleOnPageChangeListener instead of ViewPager.OnPageChangeListener and override only those methods you want to use.

viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {

    // optional 
    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }

    // optional 
    @Override
    public void onPageSelected(int position) { }

    // optional 
    @Override
    public void onPageScrollStateChanged(int state) { }
});

Hope this help :)

Edit: As per android APIs, setOnPageChangeListener (ViewPager.OnPageChangeListener listener) is deprecated. Please check this url:- Android ViewPager API

Phane answered 12/8, 2015 at 9:36 Comment(2)
As mViewPager.setOnPageChangeListener(); is deprecated now, this one is working fine.Mixologist
In the above code, to detect whether a state has changed save the current position and compare it when the page is scrolled in onPageScrolled method.Dyer
P
38

Kotlin Users,

viewPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener {

            override fun onPageScrollStateChanged(state: Int) {
            }

            override fun onPageScrolled(position: Int, positionOffset: Float, positionOffsetPixels: Int) {

            }

            override fun onPageSelected(position: Int) {
            }
        })

Update 2020 for ViewPager2

        viewPager.registerOnPageChangeCallback(object : ViewPager2.OnPageChangeCallback() {
        override fun onPageScrollStateChanged(state: Int) {
            println(state)
        }

        override fun onPageScrolled(
            position: Int,
            positionOffset: Float,
            positionOffsetPixels: Int
        ) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels)
            println(position)
        }


        override fun onPageSelected(position: Int) {
            super.onPageSelected(position)
            println(position)
        }
    })
Palaeogene answered 18/4, 2018 at 9:25 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.