我构建了一个使用标签进行导航的Android应用程序。我正在为大多数应用程序使用片段,但是我需要显示一个活动。如何在案例2中显示活动。下面是我目前的代码,我知道它会返回一个片段但是如何启用这两个片段?
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 PlaceholderFragment (defined as a static inner class below).
System.out.println("current position = " + position);
Fragment fragment = null;
switch(position){
case 0:
return new diaryFragment();
case 1:
return new newEntryFragment();
case 2:
return new Calendarnew();
}
return null;
}
答案 0 :(得分:1)
private void displayView(int position) {
// update the main content by replacing fragments
Fragment fragment = null;
switch (position) {
case 0:
fragment = new diaryFragment();
break;
case 1:
fragment = new newEntryFragment();
case 2:
fragment = new Calendarnew();
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frame_container, fragment).commit();
} else {
// error in creating fragment
Log.e("MainActivity", "Error in creating fragment");
}
答案 1 :(得分:0)
嗨,我实际上并没有得到你想要的东西,但尝试阅读这个,也许它可以以某种方式解决你的问题。 https://stackoverflow.com/a/2958586/4470313