Sil*_*vuz 6 android android-fragments android-viewpager android-nested-fragment
我正在使用SherlockFragment的嵌套片段,一切正常.但我无法将viewpager标题转换为字符串,以便我可以使用string.xml支持多语言.
这是我的代码
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;
}
}
Run Code Online (Sandbox Code Playgroud)
}
在getPageTitle我不能使用字符串并已经尝试了几种方法.
访问android.support.v4.app.FragmentPagerAdapter中的getString()? 这是一个在网站上类似的问题,但两个回答都没有让我走上正确的道路.
我试过用这个:
@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;
}
Run Code Online (Sandbox Code Playgroud)
但是不要工作.有谁知道解决这个问题?
您可以将 传递Activity给您的适配器构造函数。
mViewPager.setAdapter(new MyAdapter (getChildFragmentManager(), getActivity()));
Run Code Online (Sandbox Code Playgroud)
然后在MyAdapter:
//declare a private variable for the activity
private Activity myActivity;
public MyAdapter(FragmentManager fm, Activity myActivity) {
super(fm);
this.myActivity = myActivity;
}
Run Code Online (Sandbox Code Playgroud)
然后在你的getPageTitle
...
return myActivity.getString(R.String.title_section_scheduale1).toUpperCase(l);
...
Run Code Online (Sandbox Code Playgroud)
尝试一下希望它对你有用。
| 归档时间: |
|
| 查看次数: |
5744 次 |
| 最近记录: |