我正在测试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;
}
}
答案 0 :(得分:6)
getString(int)
仅适用于可以访问Context
- Fragments
,Activities
等的类。
鉴于这是一个Adapter
类,它不能直接访问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);
答案 1 :(得分:0)
如panini所述,需要在 Context 上调用 getString 方法,请按照以下步骤操作:
步骤1:在适配器类中,创建一个字段以在其上存储上下文。
private Context mContext
第2步:在适配器类中,调整构造函数以将Context作为第一个参数传递。
public SectionsPagerAdapter(Context context, FragmentManager fm) {
super(fm);
mContext = context;
}
第3步:在适配器类中的 getPageTitle 方法内,在上下文字段上调用 getString > mContext ,
@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());