在一个实现viewpager片段获得的getString() [英] Access to getString() in a fragment that implements a viewpager
问题描述
我使用使用SherlockFragment嵌套的片段,一切工作正常。但我不能够使viewpager标题字符串,这样我可以使用string.xml支持多国语言。
I am using a nested fragment using SherlockFragment, all works fine. But i am not being able to make viewpager title to string so that i can support multilanguage using string.xml.
下面是我的code
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;
}
}
}
在getPageTitle我可以在工作与串并已经尝试过多种方法。
on getPageTitle i can't that working with strings and already tried several methods.
<一个href=\"http://stackoverflow.com/questions/14112628/access-to-getstring-in-android-support-v4-app-fragmentpageradapter\">Access给GetString()在android.support.v4.app.FragmentPagerAdapter?
这是在网站上类似的问题,但没有回答让我在正确的道路上。
Access to getString() in android.support.v4.app.FragmentPagerAdapter? This is a question similar on the site but neither answer got me on the right path.
我试图用这样的:
@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;
}
但不起作用。任何人都知道解决这个?
But do not work. Anyone knows the solution to this?
推荐答案
您可以在活动
传递到适配器构造函数。
You can pass the Activity
to your adapter constructor.
mViewPager.setAdapter(new MyAdapter (getChildFragmentManager(), getActivity()));
然后 MyAdapter
:
//declare a private variable for the activity
private Activity myActivity;
public MyAdapter(FragmentManager fm, Activity myActivity) {
super(fm);
this.myActivity = myActivity;
}
然后在 getPageTitle
...
return myActivity.getString(R.String.title_section_scheduale1).toUpperCase(l);
...
试试吧希望它为你工作。
Try it out hope it works for you.
这篇关于在一个实现viewpager片段获得的getString()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!