简体   繁体   English

当在TabLayout中调用其他片段时,第一个片段出现第二秒钟

[英]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. 我的代码托管在这里在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 尝试setOffscreenPageLimit ,以获得更好的用户体验

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从第二个Fragment的recyclerView中选择项目时如何切换到第一个Fragment? 两个片段都在带有 viewPager2 的 tablayout 中 - How to switch to the first fragment when selecting an item from the recyclerView of second Fragment? Both fragments are in tablayout with viewPager2 我在viewpager中有布局,如何在第一个片段以外的4个片段中隐藏Bottombar? - I have tablayout with viewpager how to hide Bottombar in 4 Fragments except first Fragment? TabLayout:切换片段时滞后 - TabLayout: Lag when switching fragments 将带有片段的 Tablayout 作为子片段嵌套在导航抽屉中的片段内 - Nesting a Tablayout With Fragments as Child fragments inside an Fragment from Navigation drawer TabLayout第二次显示空白片段 - TabLayout shows a blank fragment, the second time 在Fragment中首次调用时,SharedPreferences为空 - SharedPreferences are empty when called for the first time in Fragment 未连接调试器时,TabLayout片段的实例化方式相同 - TabLayout fragments are instantiated as the same when debugger is not connected 当其他片段更改第一个片段模型时刷新片段 - Refresh fragment when other fragment changes the first's fragment model 标签布局下仅显示第一个片段 - only first fragment is being shown under tablayout 当替换为第二个片段时,第一个片段上的按钮移出屏幕 - Button on first fragment moves out of screen when replaced with second fragment
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM