我正在使用viewPage,FragmentManager和Fragment。 FragmentManager中有一个switch case来初始化每个片段。但我的片段onCreateView在我的应用程序第一次进入前景时被调用了两次。我正在附加FragmentManager类以及片段的onCreateView方法代码。请告诉我我是否做错了。
下面的是我的fragmentManager类
public class TabsPagerAdapter extends FragmentPagerAdapter {
public TabsPagerAdapter(FragmentManager fm) {
super(fm);
}
@Override
public Fragment getItem(int index) {
switch (index) {
case 0:
//Home fragment activity
System.out.println("fragment case 0");
return new LatestFragment();
case 1:
// Pages fragment activity
System.out.println("fragment case 1");
return new MostDownloadFragment();
case 2:
// Photos fragment activity
System.out.println("fragment case 2");
return new BestFragment();
case 3:
// WhatsHot fragment activity
System.out.println("fragment case 3");
return new FavouriteFragment();
default:
break;
}
return null;
}
@Override
public int getCount() {
// get item count - equal to number of tabs
return 4;
}
}
及以下是onCreateView of fragment
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_latest, container, false);
gridView = (GridView)rootView.findViewById(R.id.grid_view);
context=getActivity().getApplicationContext();
adapter = new ImageAdapter();
if (TabsArraylistData.al_latest.size() == 0) {
System.out.println("fragment latest if");
new GetCategoryAsynTask().execute();
}else {
System.out.println("fragment latest else");
gridView.setAdapter(adapter);
}
return rootView;
} // onCreateView -----------------------------------------------------
我的问题是System.out.println("片段最新如果");正在打印两次