1

我正在测试 FragmentPagerAdapter 并且我之前在一个类中拥有它。一切正常,但是一旦我分离 SectionsPagerAdapter 类,getString 在 getPageTitle 函数下不起作用。

我知道 getPageTitle 是 PagerAdapter 类的一部分,但我想知道在这个类中包含该函数的最佳方法是什么。我需要扩展课程吗?

SectionsPageAdapter 类

import java.util.Locale;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;

// A FragmentPagerAdapter that returns a fragment corresponding to one of the sections/tabs/pages. 
public class SectionsPagerAdapter extends FragmentPagerAdapter {

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

@Override  
public Fragment getItem(int position) {  
    // getItem is called to instantiate the fragment for the given page.  
    // Return a DummySectionFragment (defined as a static inner class  
    // below) with the page number as its lone argument.  
    Fragment fragment = new DummySectionFragment();  
    Bundle args = new Bundle();  
    args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);  
    fragment.setArguments(args);  
    return fragment;
}

@Override
public CharSequence getPageTitle(int position) {  
    Locale l = Locale.getDefault();  
    switch (position) {  
    case 0:  
        return getString(R.string.myFriendsTab).toUpperCase(l);  
    case 1:
        return getString(R.string.myDealsTab).toUpperCase(l);  
    case 2:
        return getString(R.string.featuredDealsTab).toUpperCase(l);
    case 3:
        return getString(R.string.browseCategoriesTab).toUpperCase(l);  
    case 4:
        return getString(R.string.localDealsTab).toUpperCase(l);  
    }  
    return null;  
}

@Override  
public int getCount() {  
     // Show 5 total pages.  
     return 5;  
}
}

MainActivity 类

import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.Menu;

public class MainActivity extends FragmentActivity {
    // Fragment PagerAdapter keeps every loaded fragment in memory. 
    // If too memory intensive, switch to FragmentStatePagerAdapter.
    SectionsPagerAdapter mSectionsPagerAdapter;

    ViewPager mViewPager; // ViewPager that will host section contents.

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Creates the adapter that will return a fragment for each of the primary sections.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager(), null);

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }
}
4

2 回答 2

7

getString(int)仅适用于有权访问 a Context- FragmentsActivities等的类。

鉴于这是一个Adapter类,它不能直接访问 a Context,因此您可能应该使用构造函数传入一个。

private Context mContext = null;

public SectionsPagerAdapter(FragmentManager fm, Context context) {
    super(fm);
    mContext = context;
}

然后使用成员字段访问getString(int)

return mContext.getString(R.string.myFriendsTab).toUpperCase(1);
于 2013-11-20T00:55:42.053 回答
0

正如帕尼尼所说,需要在Context上调用getString方法,请按照下列步骤操作:

  • 步骤1:在适配器类中,创建一个字段来存储上下文。

    private Context mContext
    
  • 步骤2:在适配器类中,调整构造函数以将Context作为第一个参数传递。

         public SectionsPagerAdapter(Context context, FragmentManager fm) {
            super(fm);
            mContext = context;
        }
    
  • 第 3 步:在适配器类中,在getPageTitle方法中,调用Context 字段mContext上的getString

    @Override
    public CharSequence getPageTitle(int position) {  
    Locale l = Locale.getDefault();  
     switch (position) {  
      case 0:  
        return mContext.getString(R.string.myFriendsTab).toUpperCase(l);  
      case 1:
        return mContext.getString(R.string.myDealsTab).toUpperCase(l);  
      case 2:
        return mContext.getString(R.string.featuredDealsTab).toUpperCase(l);
      case 3:
        return mContext.getString(R.string.browseCategoriesTab).toUpperCase(l);  
      case 4:
        return mContext.getString(R.string.localDealsTab).toUpperCase(l);  
     }  
       return null;  
    }
    
  • 第 4 步:无论您在何处使用适配器,都应将其调整为包含我们在构造函数中定义的 Context 参数。

MainActivity类中,将构造函数调整为如下所示:

    mSectionsPagerAdapter = new SectionsPagerAdapter(this, getSupportFragmentManager());
于 2018-07-01T13:07:19.043 回答