有没有办法可以在这样的片段中间使用附加按钮转到下一个书签? 我想沉迷于加载第二个标签,具体取决于第一个标签中按下的按钮。当您选择此按钮以自动移动到下一个选项卡时。 p>
编辑:
public class MainActivity extends AppCompatActivity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
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);
tabLayout.setupWithViewPager(mViewPager);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Start tab1 = new Start();
return tab1;
case 1:
Rectangle tab2a = new Rectangle();
return tab2a;
case 2:
Parameters tab3 = new Parameters();
return tab3;
case 3:
Loads tab4 = new Loads();
return tab4;
case 4:
Results tab5 = new Results();
return tab5;
default:
return null;
}
}
@Override
public int getCount() {
// Show 5 total pages.
return 5;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "START";
case 1:
return "CROSS";
case 2:
return "PARAM";
case 3:
return "LOADS";
case 4:
return "RESULT";
}
return null;
}
}
}
public class Start extends Fragment{
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.start_tab1, container, false);
final Button button = (Button) rootView.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Rectangle");
}
});
final Button button2 = (Button) rootView.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
System.out.println("Circle");
}
});
return rootView;
};
}
答案 0 :(得分:0)
点击按钮即可简单地调用此API。
mViewpager.setCurrentItem(int index)//0 for first tab, 1 for second tab and 3 for third tab.
您需要使用标签切换viewpager。
tabs.getTabAt(int index).select();
答案 1 :(得分:0)
将您的ViewPager
设为公共字段
public ViewPager mViewPager;
然后在片段按钮clickListener
中调用此方法((MainActivity) getActivity()).mViewPager.setCurrentItem(position);