سؤال

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.

Here is my 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;
    }
}

}

on getPageTitle i can't that working with strings and already tried several methods.

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.

I tried using this:

@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()));

then in MyAdapter:

//declare a private variable for the activity
private Activity myActivity;
public MyAdapter(FragmentManager fm, Activity myActivity) {
    super(fm);
    this.myActivity = myActivity;
}

then in your getPageTitle

...
return myActivity.getString(R.String.title_section_scheduale1).toUpperCase(l);
...

Try it out hope it works for you.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top