如何在我的View寻呼机中的标签之间切换,以便加载特定的片段?

时间:2017-01-09 14:47:23

标签: java android android-fragments android-viewpager

我有一个带3个按钮的主要活动。 3个按钮将带您进入选项卡式活动。第一个按钮将您带到选项卡1,这是默认选项卡。我需要什么代码,所以当我按下按钮2时,它会将我带到选项卡2.按钮3会将我带到选项卡3,依此类推。请不要只是说传递一个额外的。我已经尝试过,那个废话不起作用。非常感谢一个坚实的答案,坚持了几个月。

MainActivity.java

 public void TakeToTab (View view) {

    //GOTO defa
    Intent i = new Intent(this, TabbedActivity.class);
    startActivityForResult(i, 0);
}

public void TakeToTab2 (View view) {

    //GOTO tab2
    Intent i = new Intent(getApplicationContext(),
            TabbedActivity.class);

    i.putExtra("Tab2", 2);
    startActivity(i);


}

public void TakeToTab3 (View view) {

 //GOTO tab3
    Intent i = new Intent(getApplicationContext(),
            TabbedActivity.class);

    i.putExtra("Tab3", 2);
    startActivity(i);

}

}

TabbedActivity.java

public class TabbedActivity extends AppCompatActivity {

private SectionsPagerAdapter mSectionsPagerAdapter;

private ViewPager mViewPager;

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

//This right?
    int position1 = getIntent().getExtras().getInt("Tab1");
    int position2 = getIntent().getExtras().getInt("Tab2");
    int position3 = getIntent().getExtras().getInt("Tab3");


    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);

    mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

    // Set up the ViewPager with the sections adapter.
    mViewPager = (ViewPager) findViewById(R.id.container);
    mViewPager.setAdapter(mSectionsPagerAdapter);

//This right?
    mViewPager.setCurrentItem(position1);
    mViewPager.setCurrentItem(position2);
    mViewPager.setCurrentItem(position3);

0 个答案:

没有答案