现在我正在尝试使用导航抽屉和滑动视图来编写一个 android 应用程序。在导航抽屉中选择“时间表”时,应用程序会打开一个滑动视图,其中包含 6 个页面,每个页面上都有页码。问题是 - 在导航抽屉中重新选择页面“时间表”时 - 显示第 5 和第 6 页,但不会加载内容(片段)。
我使用了滑动视图示例的代码:http: //developer.android.com/training/implementing-navigation/lateral.html
这应该是带有滑动视图的片段:
public class Timetable extends Fragment {
ViewPager mViewPager;
SectionsPagerAdapter mSectionsPagerAdapter;
public TimeTable() {
// Empty constructor required for fragment subclasses
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.screen_stundenplantag,
container, false);
mSectionsPagerAdapter = new SectionsPagerAdapter(this.getActivity()
.getSupportFragmentManager());
mViewPager = (ViewPager) rootView.findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
return rootView;
}
这是 SectionsPagerAdapter 类:
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);
fragment.setArguments(args);
return fragment;
}
@Override
public int getCount() {
// Show 6 total pages.
return 6;
}
@Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
case 4:
return getString(R.string.title_section5).toUpperCase(l);
case 5:
return getString(R.string.title_section6).toUpperCase(l);
}
return null;
}
}
最后但并非最不重要的是 DummySectionFragment 类:
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static String ARG_SECTION_NUMBER = "";
public DummySectionFragment() {
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main_dummy,
container, false);
TextView dummyTextView = (TextView) rootView
.findViewById(R.id.section_label);
dummyTextView.append("Page "
+ Integer.toString(getArguments()
.getInt(ARG_SECTION_NUMBER)));
Log.i(this.getClass().getSimpleName(),String.valueOf(getArguments().get(DummySectionFragment.ARG_SECTION_NUMBER)));
switch (getArguments().getInt(ARG_SECTION_NUMBER)) {
case 1:
break;
case 2:
break;
case 3:
break;
case 4:
break;
case 5:
break;
case 6:
break;
}
return rootView;
}
}