简体   繁体   中英

First fragment appears for a second when other fragments are called in tablayout

I have created an news app, with tab view. Based on the position of the tabs, the fragment for the selected category must appear. This happens, but the first fragment (tech) appears for a second and only then the other category fragment appears when the tab is switched by the user.

Sorry if I didn't explain it properly, a video showing the above issue in action is linked here .

My code is hosted here on github.

public Fragment getItem(int position) {

        if ( position == 0 ) {

            TechFragment tech = new TechFragment();
            return tech;

        } else if ( position == 1 ) {
            GeneralFragment general = new GeneralFragment();
            return general;
        }
        else if(position == 2){

            SportsFragment sports = new SportsFragment();
            return sports;
        }
        else if(position == 3){

            BusinessFragment business = new BusinessFragment();
            return business;
        }
        else if(position == 4){

            AppleFragment apple = new AppleFragment();
            return apple;
        }
        else if(position == 5){

            BitcoinFragment bitcoin = new BitcoinFragment();
            return bitcoin;
        }
        else if(position == 6){

            EntertainmentFragment entertainment = new EntertainmentFragment();
            return entertainment;
        }
        else if(position == 7){

            SportFragment sport = new SportFragment();
            return sport;
        }
        else if(position == 8){

            HealthFragment health = new HealthFragment();
            return health;
        }
        else{
            ScienceFragment science = new ScienceFragment();
            return science;
        }
    }

Thanks in advance.

Try setOffscreenPageLimit , for better user experience

mViewPager.setAdapter(mSectionsPagerAdapter);
mViewPager.setOffscreenPageLimit(mSectionsPagerAdapter.getCount());

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM