访问实现viewpager的片段中的getString()

时间:2014-04-26 03:07:50

标签: 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;
    }
}

}

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

但是不要工作。有谁知道解决这个问题?

1 个答案:

答案 0 :(得分:4)

您可以将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);
...

尝试一下希望它适合你。