这是我在导航菜单中的片段。
class MirandaFragment extends Fragment {
protected View view;
FragmentPagerAdapter adapterViewPager;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
view = inflater.inflate(R.layout.miranda, container, false);
ViewPager vpPager = (ViewPager) view.findViewById(R.id.vpPager);
adapterViewPager = new MyPageAdapter(getFragmentManager());
vpPager.setAdapter(adapterViewPager);
return view;
}
}
这是我设置两个片段EngMiranda和SpaMiranda的适配器。
class MyPageAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;
public MyPageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages.
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for a particular page.
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return EngMiranda.newInstance("Fragment 1", getCount());
case 1:
return SpaMiranda.newInstance("Fragment 2", getCount());
default:
return null;
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "English";
case 1:
return "Spanish";
default:
return null;
}
}
}
应用程序第一次打开后,我可以打开片段并查看其中的选项卡片段,但是当我们第二次打开该片段时,适配器不会显示。