【问题标题】:How can I access localised strings from within a static FragmentPagerAdapter如何从静态 FragmentPagerAdapter 中访问本地化字符串
【发布时间】:2014-09-20 18:44:49
【问题描述】:

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

两个选项卡和astuetz's pagerSlidingTabStrip 工作正常,但适配器的 getPageTitle 方法无法访问字符串资源,只能为每个选项卡标题返回一个硬编码字符串(参见代码底部)。当我尝试访问 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";
        }
    }
}

}

【问题讨论】:

    标签: android static android-viewpager fragmentpageradapter pagerslidingtabstrip


    【解决方案1】:

    在你的情况下,你必须更换:

    public static class MyAccountAdapter extends FragmentPagerAdapter
    

    public class MyAccountAdapter extends FragmentPagerAdapter
    

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

    【讨论】:

    • 哇,如此简单,如此正确!我删除了 static 关键字,效果很好。不过,我仍然不明白,为什么文档提出了“公共静态类”与普通的“公共类”
    • 也许他们根本不需要访问父类的实例方法或字段...
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-11
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多