大家好,我做了一个天气应用程序,我有3个标签 我想在运行应用程序时将选项卡2设为默认选项卡
这是我的代码:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position)
{
case 0:
Tab1 tab1=new Tab1();
return tab1;
case 1:
Tab2 tab2=new Tab2();
return tab2;
case 2:
Tab3 tab3=new Tab3();
return tab3;
}
return null;
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "NextDay";
case 1:
return "Today";
case 2:
return "3rd Day";
}
return null;
}
}
答案 0 :(得分:1)
在您的托管活动中,onCreate
致电mViewPager.setCurrentItem(1);
答案 1 :(得分:0)
你可以选择这个。在主机活动中设置适配器后,应将当前项设置为1。
public class MyActivity extends AppCompatActivity{
SectionsPagerAdapter pagerAdapter;
ViewPager myViewPager;
public static final int PAGER_CURRENT_ITEM = 1;
@Override
protected void onCreate(Bundle savedInstanceState){
setContentView(R.layout.activity_main);
myViewPager = (ViewPager) findViewById(R.is.view_pager_id);
pagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter)
viewPager.setCurrentItem(PAGER_CURRENT_ITEM);
}
}