如何从静态FragmentPagerAdapter中访问本地化字符串

时间:2014-09-20 18:44:49

标签: android static android-viewpager fragmentpageradapter pagerslidingtabstrip

我已经关注official documentation on how to implement swipe views with TabStrip instead of Tabs以创建包含嵌套片段(FragmentMyProfile.class和FragmentMyLibrary.class)的片段(FragmentMyAccount.class)。这些嵌套片段对应于childFragmentManager管理的两个选项卡。

两个选项卡和astuetz's pagerSlidingTabStrip工作正常,但适配器的getPageTitle方法无权访问String资源,并且只能为每个选项卡标题返回一个硬编码字符串(请参阅代码底部)。我尝试访问xml资源时出现的编译时错误读取:

"FragmentMyAccount.this cannot be referenced from a static context"

如果下面的代码无法调整,那么替代实现是否足以实现我的目标?

package com.kouretinho.myapp;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.astuetz.PagerSlidingTabStrip;


public class FragmentMyAccount extends Fragment {

private static final int NUM_ITEMS = 2;

MyAccountAdapter mAdapter;
ViewPager mPager;
PagerSlidingTabStrip mTabStrip;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
}

@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
    View rootView = inflater.inflate(R.layout.fragment_my_account, container, false);

    // Initialize the FragmentPager Adapter
    mAdapter = new MyAccountAdapter(getChildFragmentManager());

    // Initialize the ViewPager and set an adapter
    mPager = (ViewPager) rootView.findViewById(R.id.viewpager_my_account);
    mPager.setAdapter(mAdapter);

    // Initialize the TabStrip and bind the tabs to the ViewPager
    mTabStrip = (PagerSlidingTabStrip) rootView.findViewById(R.id.viewpager_tab_strip);
    mTabStrip.setViewPager(mPager);

    return rootView;
}

@Override
public void onActivityCreated(@Nullable Bundle savedInstanceState) {
    super.onActivityCreated(savedInstanceState);
}

@Override
public void onSaveInstanceState(Bundle outState) {
    super.onSaveInstanceState(outState);
}

public static class MyAccountAdapter extends FragmentPagerAdapter{

    private static final int MY_PROFILE = 0;
    private static final int MY_LIBRARY = 1;
    private static final String sExceptionMessage = "FragmentPagerAdapter was asked to getItem " +
            "with position other than 0 or 1. This FragmentPagerAdapter can only return one of" +
            "two Fragments at position 0 or 1.";

    public MyAccountAdapter(FragmentManager fm){
        super(fm);
    }

    @Override
    public int getCount() {
        return FragmentMyAccount.NUM_ITEMS;
    }

    @Override
    public Fragment getItem(int position) {
        switch (position){
            case MY_PROFILE:
                return new UserProfileFragment();
            case MY_LIBRARY:
                return new UserBooksFragment();
            default:
                throw new IllegalStateException(sExceptionMessage);
        }
    }

    @Override
    public CharSequence getPageTitle(int position) {

        switch (position){
            case MY_PROFILE:
// **********  THIS LINE IS PROBLEMATIC, cannot get Localised String resources
               return  FragmentMyAccount.this.getActivity().getResources().getString(R.id.my_profile);
            case MY_LIBRARY:
                return "my_library";
            default:
                return "no_title";
        }
    }
}

}

1 个答案:

答案 0 :(得分:1)

在您的情况下,您必须替换:

public static class MyAccountAdapter extends FragmentPagerAdapter

public class MyAccountAdapter extends FragmentPagerAdapter

解决此问题的另一种方法是在构造函数中将Context传递给MyAccountAdapter(就像使用FragmentManager一样)。