0

为了创建包含嵌套片段(FragmentMyProfile.class 和 FragmentMyLibrary.class)的片段(FragmentMyAccount.class),我遵循了有关如何使用 TabStrip 而不是 Tabs 实现滑动视图的官方文档。这些嵌套片段对应于由 childFragmentManager 管理的两个选项卡。

两个选项卡和astuetz 的 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";
        }
    }
}

}

4

1 回答 1

1

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

public static class MyAccountAdapter extends FragmentPagerAdapter

public class MyAccountAdapter extends FragmentPagerAdapter

解决此问题的另一种方法是传递ContextMyAccountAdapterin 构造函数(就像使用 FragmentManager 一样)。

于 2014-09-20T18:57:30.163 回答