0

我遵循official documentation on how to implement swipe views with TabStrip instead of Tabs以創建包含嵌套片段(FragmentMyProfile.class和FragmentMyLibrary.class)的Fragment(FragmentMyAccount.class)。這些嵌套的片段對應於由childFragmentManager管理的兩個選項卡。如何從靜態FragmentPagerAdapter中訪問本地化的字符串

這兩個選項卡和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"; 
     } 
    } 
} 

}

回答

1

在你的情況,你必須更換:

public static class MyAccountAdapter extends FragmentPagerAdapter 

public class MyAccountAdapter extends FragmentPagerAdapter 

另一種方式來解決這個問題是通過Context在構造MyAccountAdapter(如你用FragmentManager做)。

+0

哇,那麼簡單,那麼正確!我刪除了靜態關鍵字,它工作得很好。我仍然不明白,爲什麼文檔提出一個「公共靜態類」vs一個普通的「公共類」 – kouretinho 2014-09-20 19:33:59

+1

也許他們根本不需要訪問父類的實例方法或字段...... – Ayzen 2014-09-20 19:37:05

相關問題