我正在使用SherlockFragment嵌套的 fragment ,一切正常。但是我无法使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;
    }
}

}

在getPageTitle上,我不能使用字符串,并且已经尝试了几种方法。

Access to getString() in android.support.v4.app.FragmentPagerAdapter?
这是一个与网站上类似的问题,但没有一个答案能使我走上正确的道路。

我尝试使用此:
@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;
    }

但是不行。有人知道解决方案吗?

最佳答案

您可以将Activity传递给适配器构造函数。

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);
...

尝试一下,希望它对您有用。

10-08 03:01