我想在标签之间切换而不向左或向右滑动。使用下面的类,这是Android Studio活动之一,我通过使用外部事件尝试从一个选项卡移动到另一个选项卡。
我该怎么办?我应该采用哪些方法/覆盖?
public class Tabbed extends Activity {
private SectionsPagerAdapter mSectionsPagerAdapter;
private ViewPager mViewPager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
//getMenuInflater().inflate(R.menu.menu_tabbed, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_tabbed, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
}
return null;
}
}
}
也许我可以用:
@Override public void changeTab(int where) {
mViewPager.setCurrentItem(mViewPager.getCurrentItem()+where);
}
其中changeTab()
方法是mainActivity中定义的接口。
我也在我的mainActivity中:
intent=new Intent(this, Tabbed.class);
startActivity(intent);
如何从mainActivity调用changeTab()
方法?
答案 0 :(得分:0)
您可以使用ViewPager
对象的setCurrentItem()功能。
示例:
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setCurrentItem(2); // set number of desired tab
编辑:对于你的第二个问题,StackOverflow上已经有很多问题。您不能只从另一个活动中调用方法。您可以向意图添加额外数据以强制活动启动功能。但这不适用于您,因为您希望在创建intent之后调用该函数。我想你必须看一下听众的问题以及你们之间的活动沟通。 Here是一个与您的问题密切相关的问题,可以帮助您解决问题。