android-如何更改滑动标签的默认位置

android-How to change sliding tab default position

这是我制作滑动标签的代码。它完美地制作了标签。

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.firstactivity);

     // Get the ViewPager and set it's PagerAdapter so that it can display items
    ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
    viewPager.setAdapter(new SampleFragmentPagerAdapter(getSupportFragmentManager(),FistActiivty.this));

    // Give the SlidingTabLayout the ViewPager
    SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
    // Center the tabs in the layout
    slidingTabLayout.setDistributeEvenly(true);
    slidingTabLayout.setViewPager(viewPager);
    slidingTabLayout.setCustomTabColorizer(new SlidingTabLayout.TabColorizer() {
        @Override
        public int getIndicatorColor(int position) {
            return Color.RED;
            //or return getResources().getColor(R.color.red);
        }
    });

}

class SampleFragmentPagerAdapter extends FragmentPagerAdapter {
    final int PAGE_COUNT = 4;
    private String tabTitles[] = new String[] { "Tab1", "Tab2", "Tab3" , "Tab4"};
    private Context context;

    public SampleFragmentPagerAdapter(FragmentManager fm, Context context) {
        super(fm);
        this.context = context;
    }

    @Override
    public int getCount() {
        return PAGE_COUNT;
    }

    @Override
    public Fragment getItem(int position) {
        Fragment fragment=null; 
        if (position==0){
            fragment=new Fragment_A();
        }
        if (position==1){
            fragment=new Fragment_B();
        }
        if (position==2){
            fragment=new Fragment_C();
        }
        if (position==3){
            fragment=new Fragment_C();
        }
        return fragment;
    }

    @Override
    public CharSequence getPageTitle(int position) {
        // Generate title based on item position
        return tabTitles[position];
    }
}

问题是,如何在 activity 运行时更改默认选择的选项卡? 当 activity 打开时,如何将默认选项卡位置更改为第三?

使用这个,它会select加载一个特定的标签

mViewPager.setCurrentItem(position);