你可以測試的另一個問題是這個。對於這個測試,我創建了一個帶有製表符的活動,並且每次用戶選擇一個製表符時,操作欄中的字幕都會根據不同的語言(英文/西班牙文)進行更改。因此,進出口猜測你的活動結構是這樣的:
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看,我有人稱動作欄setSubtitle
方法,因此,每次用戶選擇一個新的頁面,我的行動吧字幕改變,則適配器:
/**
* 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;
}
}
我希望這可以幫助你!
沒有人知道嗎? – 2012-08-17 08:35:21