所以我有一个奇怪的问题。基本上我有一个带有四个按钮的Main活动,每个按钮打开一个不同的选项卡(有4个选项卡)。现在,如果我点击第4个按钮,我会转到第4个片段,但如果我尝试点击选项卡式活动中的第一个标签,则它不会执行任何操作。
所以如果我从第2个第3个或第4个开始,我就不能去第一个标签,除非我先点击任何其他标签。
这只是第一个像这样工作的标签,这让我觉得可能有一些值应该在菜单按钮点击上改变但不是吗?
这是我的TabsActivity onCreate:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabs);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
//
int defaultValue = 5;
//int page = getIntent().getIntExtra("One", defaultValue);
mViewPager.setCurrentItem(getIntent().getIntExtra("One", defaultValue));
//
//
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
//
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager))
此处的TabsActivity类负责选择片段:
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position){
case 0:
tab1recipes tab1 = new tab1recipes();
return tab1;
case 1:
tab2timer tab2 = new tab2timer();
return tab2;
case 2:
tab3measure tab3 = new tab3measure();
return tab3;
case 3:
tab4substit tab4 = new tab4substit();
return tab4;
default:
return null;
}
}
这是MainActivity:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn1 = (Button) findViewById(R.id.recipesbtn);
Button btn2 = (Button) findViewById(R.id.timerbtn);
Button btn3 = (Button) findViewById(R.id.measurebtn);
Button btn4 = (Button) findViewById(R.id.subsbtn);
btn1.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int page=0;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn2.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int page=1;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn3.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int page=2;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
btn4.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
int page=3;
Intent intent = new Intent(MainActivity.this, TabsActivity.class);//startActivity();
intent.putExtra("One", page);
startActivity(intent);
}
});
答案 0 :(得分:0)
好的,我终于修好了。这是如何(以防有人需要它):
mViewPager.setCurrentItem(getIntent().getIntExtra("One", defaultValue));
之后
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
现在我觉得非常愚蠢