SlidingTabLayout,重新启动片段在案例4到1?

时间:2016-02-08 13:19:55

标签: java android pagerslidingtabstrip

我的SlidingTabLayout在从片段标签切换到片段仪表板或位置4到0时重新启动我的片段仪表板活动。 这是我到目前为止编写的代码,任何人都可以对这种情况有所了解吗?

 public class SectionsPagerAdapter extends FragmentStatePagerAdapter {
    public SectionsPagerAdapter(FragmentManager fm) {
        super(fm);
    }

    @Override
    public Fragment getItem(int position) {
        //2 steps creating a fragment, create the fragment class and create the UI for it
        Fragment fragment = null;
        //we dont want it to return our place holder Fragment instead we want to switch bewtween 3
        //create a switch case based on the position applied or the fragment selected
        switch (position) {
            case HOME:
                fragment = Dashboard.newInstance("","");
                break;
            case TAGS:
                fragment = Tags.newInstance("","");
                break;
            case MY_RECENT:
                fragment = MyPosts.newInstance("","");
                break;
            case TOP:
                fragment = Top.newInstance("", "");
                break;
        }
        return fragment;
    }

    @Override
    public int getCount() {
        // Show 3 total pages.
        return 4;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        switch (position) {
            case 0:
                return "Home";
            case 1:
                return "Top";
            case 2:
                return "Recent";
            case 3:
                return "Tags";
        }
        return null;
    }
}

1 个答案:

答案 0 :(得分:1)

你必须一次加载所有片段,你应该调用这个方法

int size=4;
viewPager.setOffscreenPageLimit(size);

设置适配器后,大小是您要第一次加载的片段的数量