Sto usando un frammento annidato usando SherlockFragment, tutto funziona correttamente. Ma non riesco a rendere viewpager il titolo alla stringa in modo da poter supportare il multilinguaggio usando string.xml.Accesso a getString() in un frammento che implementa un viewpager
Ecco il mio codice
public class schedule_pl extends SherlockFragment {
private static String titles[] = new String[] { "Portugal", "Lisbon" , "Azores" };
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.main_pager, container, false);
// Locate the ViewPager in viewpager_main.xml
ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
// Set the ViewPagerAdapter into ViewPager
mViewPager.setAdapter(new MyAdapter (getChildFragmentManager()));
return view;
}
@Override
public void onDetach() {
super.onDetach();
try {
Field childFragmentManager = Fragment.class
.getDeclaredField("mChildFragmentManager");
childFragmentManager.setAccessible(true);
childFragmentManager.set(this, null);
} catch (NoSuchFieldException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
}
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int position) {
switch(position)
{
case 0:
return new place();
case 1:
return new place2();
case 2:
return new place3();
}
return null;
}
@Override
public CharSequence getPageTitle(int position) {
return titles[position];
}
@Override
public int getCount() {
// Show 3 total pages.
return 3;
}
}
public static class place extends Fragment {
public place() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment1, container,
false);
return rootView;
}
}
public static class place3 extends Fragment {
public place3() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment2, container,
false);
return rootView;
}
}
public static class place2 extends Fragment {
public place2() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment3, container,
false);
return rootView;
}
}
}
su getPageTitle non posso che lavorare con le stringhe e già provato diversi metodi.
Access to getString() in android.support.v4.app.FragmentPagerAdapter? Questa è una domanda simile sul sito ma nessuna risposta mi ha fatto sulla strada giusta.
Ho provato ad utilizzare questo:
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getcontext.getString(R.string.title_section_schedule1).toUpperCase(l);
case 1:
return getcontext.getString(R.string.title_section_schedule2).toUpperCase(l);
case 2:
return getcontext.getString(R.string.title_section_schedule3).toUpperCase(l);
}
return null;
}
Ma non funzionano. Qualcuno conosce la soluzione a questo?
Ha funzionato per me! Grazie;) – Silvuz
felice di essere stato in grado di aiutare :) – Coderji