这是我制作滑动标签的代码。它完美地制作标签。
@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];
}
}
问题是,如何在活动运行时更改默认选定的标签? 当活动打开时,如何将默认选项卡位置更改为第三位?
答案 0 :(得分:10)
使用此选项,它将在加载时选择特定选项卡
mViewPager.setCurrentItem(position);