2012-07-31 154 views
2

我正在使用FragmentPagerAdapter,我想用不同語言設置標題信息(「新聞,信息,幫助,Memebership」 - 請參閱下面的代碼)。問題是如何從res/values/strings.xml中讀取標題?fragmentpageradapter中的多語言標題

有人可以幫助我嗎?

感謝

湯姆

public class MainInfoViewPagerAdapter extends FragmentPagerAdapter implements PageIndicator 
{ 
private String[] titles= new String[] 
     {   
     "News",   
     "Information",   
     "Help", 
     "Membership" 
     }; 
public MainInfoViewPagerAdapter(FragmentManager fm) 
{ 
    super(fm); 
}   
+0

沒有人知道嗎? – 2012-08-17 08:35:21

回答

0

我管理它創建一個私人SparseArray<String> TITLES;,並在主要活動填補它的OnCreate中的功能,這樣的事情:

TITLES = new SparseArray<String>(); 
    TITLES.put(KEY_FRAGMENT, 
      getResources().getString(R.string.subtitle_keys)); 
    TITLES.put(CERTIFICATE_FRAGMENT, 
      getResources().getString(R.string.subtitle_cert)); 
    TITLES.put(TRUST_NETWORK_FRAGMENT, 
      getResources().getString(R.string.subtitle_trust_network)); 
    TITLES.put(CRL_FRAGMENT, getResources() 
      .getString(R.string.subtitle_crl)); 
    TITLES.put(SECURE_FRAGMENT, 
      getResources().getString(R.string.subtitle_secure)); 
    TITLES.put(SETTINGS_FRAGMENT, 
      getResources().getString(R.string.subtitle_settings)); 

而且finaly在getTitle中的FragmentPagerAdapter做這樣的事情:

@Override 
    public CharSequence getPageTitle(int position) { 
     switch (position) { 
     case 0: 
      // Fragment of the app is the Key Management. 
      return TITLES.get(position); 
     case 1: 
      // Fragment of the app is the Certificate Management. 
      return TITLES.get(position); 

     case 2: 
      // Fragment of the app is the Trust Network Management. 
      return TITLES.get(position); 

     case 3: 
      // Fragment of the app is the CRL Management. 
      return TITLES.get(position); 

     default: 
      return TITLES.get(position); 
     } 
    } 

也許它不是最好的方式,但它的一種方式!

+0

Hi Javier,在getTitle()的FragmentPagerAdapter中,「TITLES無法解析」。我能做什麼?最好的問候湯姆 – 2012-09-02 20:15:05

+0

你是否創建FragmentPagerAdapter作爲主Activity類的內部類rigth?如果這樣做,只需將TITLES聲明爲主類的屬性,以便內部類可以訪問它。這是一個問題,但我會發佈一個我已經測試過的應用程序的其他aproach。 – JavierSP1209 2012-09-04 04:09:39

0

你可以測試的另一個問題是這個。對於這個測試,我創建了一個帶有製表符的活動,並且每次用戶選擇一個製表符時,操作欄中的字幕都會根據不同的語言(英文/西班牙文)進行更改。因此,進出口猜測你的活動結構是這樣的:

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; 
    } 
} 

我希望這可以幫助你!

3

這是我如何解決它:

在適配器:

public class TitleAdapter extends FragmentPagerAdapter { 
    private String titles[]; 
    private Fragment frags[]; 

    public TitleAdapter(FragmentManager fm, Context context) { 
       super(fm); 

       Resources resources = context.getResources(); 
       titles = resources.getStringArray(R.array.titles); 

       frags = new Fragment[titles.length]; 

       frags[0] = new NewsFragment(); 
       frags[1] = new ArtistsFragment(); 
       frags[2] = new InfoFragment(); 
       frags[3] = new EatDrinkFragment(); 

     .... 

/values/arrays.xml是這樣的:

<string-array name="titles"> 
     <item>NEWS</item> 
     <item>ARTISTS</item> 
     <item>INFORMATION</item> 
     <item>EAT AND DRiNK</item> 
    </string-array> 

如果你想的語言,只需添加另一個值文件夾(如/values-fr/strings.xml)作爲法語。