您可以测试的另一种方法是这个。对于这个测试,我创建了一个带有选项卡的活动,每次用户选择一个选项卡时,操作栏中的字幕都会更改为不同的语言(英语/西班牙语)。所以我猜你的 Activity 结构是这样的:
MainActivity extends FragmentActivity
- In this class you have one ViewPager and one PagerAdapter as attributes
- PagerAdapter is a inner class inside MainActivity.
代码是这样的,首先是主要活动:
public class TrustNetworkActivity extends FragmentActivity implements
ActionBar.TabListener {
AppSectionsPagerAdapter mAppSectionsPagerAdapter;
private SparseArray<String> TITLES;
ViewPager mViewPager;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DataBaseDictionary.PACKAGE_NAME = getApplicationContext()
.getPackageName();
//Loads the data base
initDBLoader(this);
fillSubtitlesArray();
// Create the adapter that will return a fragment for each of the three
// primary sections
// of the app.
mAppSectionsPagerAdapter = new AppSectionsPagerAdapter(
getSupportFragmentManager());
// Set up the action bar.
final ActionBar ab = getSupportActionBar();
// ab.setDisplayHomeAsUpEnabled(Boolean.TRUE);
ab.setSubtitle(TITLES.get(KEY_FRAGMENT));
ab.setDisplayShowTitleEnabled(true);
// Set up the ViewPager, attaching the adapter and setting up a listener
// for when the user swipes between sections.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mAppSectionsPagerAdapter);
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
@Override
public void onPageSelected(int position) {
// When swiping between different app sections, select
// the corresponding tab.
// We can also use ActionBar.Tab#select() to do this if
// we have a reference to the
// Tab.
ab.setSubtitle(TITLES.get(position));
ab.setSelectedNavigationItem(position);
}
});
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_keys)
.setTabListener(this));
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_cert)
.setTabListener(this));
ab.addTab(ab.newTab().setText("")
.setIcon(R.drawable.ic_menu_trust_network).setTabListener(this));
ab.addTab(ab.newTab().setText("").setIcon(R.drawable.ic_menu_crl)
.setTabListener(this));
ab.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
}
/**
* Fills the subtitle array with the resource strings, so it would be
* multilanguage
*/
private void fillSubtitlesArray() {
TITLES = new SparseArray<String>();
TITLES.put(0,
getResources().getString(R.string.subtitle_keys));
TITLES.put(1,
getResources().getString(R.string.subtitle_cert));
TITLES.put(2,
getResources().getString(R.string.subtitle_trust_network));
TITLES.put(3, getResources()
.getString(R.string.subtitle_crl));
TITLES.put(4,
getResources().getString(R.string.subtitle_secure));
TITLES.put(5,
getResources().getString(R.string.subtitle_settings));
}
正如你在OnPageChangeListener
ViewPager 中看到的,我调用了 action barsetSubtitle
方法,所以每次用户选择一个新页面时,我的 action bar 的副标题都会改变,然后适配器:
/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the primary sections of the app.
*/
public class AppSectionsPagerAdapter extends FragmentStatePagerAdapter {
public AppSectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int i) {
switch (i) {
case 0:
// Fragment of the app
return new Fragment1();
case 1:
// Fragment of the app
return new Fragment2();
case 2:
// Fragment of the app
return new Fragment3();
case 3:
// Fragment of the app
return new Fragment4();
default:
return new Fragment5();
}
}
@Override
public int getCount() {
return 4;
}
}
我希望这可以帮助你!