在特定标签栏中打开应用

时间:2015-03-10 10:12:48

标签: android tabs

我有一个带导航抽屉和6个寻呼机滑动标签的应用。当我启动应用程序时,左侧选项卡打开。我希望它打开特定标签,我该怎么做?谢谢。

这是ViewPagerFragment:

        mTabs.add(new SamplePagerItem(0, getString(R.string.tab_one), getResources().getColor(Utils.colors[4]),  Color.GRAY));
        mTabs.add(new SamplePagerItem(1, getString(R.string.tab_two), getResources().getColor(Utils.colors[4]), Color.GRAY));
        mTabs.add(new SamplePagerItem(2, getString(R.string.tab_three), getResources().getColor(Utils.colors[4]), Color.GRAY));
        mTabs.add(new SamplePagerItem(3, getString(R.string.tab_four), getResources().getColor(Utils.colors[4]),  Color.GRAY));
        mTabs.add(new SamplePagerItem(4, getString(R.string.tab_five), getResources().getColor(Utils.colors[4]), Color.GRAY));
        mTabs.add(new SamplePagerItem(5, getString(R.string.tab_six), getResources().getColor(Utils.colors[4]), Color.GRAY));

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        return inflater.inflate(R.layout.viewpager_fragment, container, false);
    }

    @Override
    public void onViewCreated(View view, Bundle savedInstanceState) {
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.mPager);

        mViewPager.setOffscreenPageLimit(4); 
        mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), mTabs));

        SlidingTabLayout mSlidingTabLayout = (SlidingTabLayout) view.findViewById(R.id.mTabs);
        mSlidingTabLayout.setBackgroundResource(R.color.white);
        mSlidingTabLayout.setViewPager(mViewPager);

        mSlidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {

            @Override
            public int getIndicatorColor(int position) {
                return mTabs.get(position).getIndicatorColor();
            }

            @Override
            public int getDividerColor(int position) {
                return mTabs.get(position).getDividerColor();
            }
        });
    }
}

1 个答案:

答案 0 :(得分:1)

你可以试试这个

mViewPager.setCurrentItem(position);