Vince V.
Vince V.

Reputation: 3143

Is there a tabLoaded callback in an android viewpager?

Currently I'm building an app with different tabs in it. In one of the tabs I have a camera fragment that I start and show to the user. To implement smooth swiping I want to load the tab content itself, so that the user his swiping doesn't hang. When it's loaded the camera should be started.

Is there some sort of a callback/listener/trick that can be used to see if a fragment is fully loaded?

Upvotes: 2

Views: 1057

Answers (2)

PEHLAJ
PEHLAJ

Reputation: 10126

Tab layout is bound to view pager using tablayout.setupWithViewPager(viewPager), so you can use view pager page listener to detect tab loaded event.

Official Documentation

If you're using a ViewPager together with TabLayout, you can call setupWithViewPager(ViewPager) to link the two together.

viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {

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

        }

        @Override
        public void onPageSelected(int position) {

            //tab loaded
        }

        @Override
        public void onPageScrollStateChanged(int state) {

        }
    });`

Upvotes: 1

seanjfarrell
seanjfarrell

Reputation: 241

It appears that many people are using setOnPageChangedListener() on the ViewPager. We ended up implementing this after noticing a race condition due to Fragments not loading synchronously due to using the FragmentManager. Check out the answer here.

Which lifecycle callback is called when a fragment pager adapter's fragment comes to screen?

Upvotes: 0

Related Questions