在我的应用程序中我有一个Activity并且有一些片段,我可以在片段之间切换而没有任何问题。其中一个片段有ViewPager
和其他一些片段,在父片段之间切换abd回到嵌套片段与viewPager,嵌套片段不显示,我必须关闭应用程序并再次运行以显示。
主要活动:
public void updateFragment(int selectedItem) {
mFragment = null;
fts = getSupportFragmentManager().beginTransaction();
switch (selectedItem) {
case 0:
mFragment = new FragmentMainView();
break;
case 1:
mFragment = new FragmentAddNewWayBill();
break;
}
fts.replace(R.id.mainViewFragments, mFragment, "0");
fts.commit();
mDrawerState = false;
}
在这段代码中,我可以在片段之间切换。 FragmentMainView()
让ViewPager包含一些已定义的片段。
public class FragmentMainView extends Fragment {
private FragmentActivity myContext;
private ViewPager vpPager;
private FragmentManager fragManager;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main_view, container, false);
G.currentActivity = FragmentMainView.this.getActivity();
fragManager = myContext.getSupportFragmentManager();
vpPager = (ViewPager) view.findViewById(R.id.vpPager);
MyPagerAdapter adapterViewPager = new MyPagerAdapter(fragManager);
vpPager.setAdapter(adapterViewPager);
return view;
}
public static class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 2;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
@Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return FragmentOne.newInstance(0);
case 1:
return FragmentTwo.newInstance(1);
default:
return null;
}
}
// Returns the page title for the top indicator
@Override
public CharSequence getPageTitle(int position) {
String tabHeaderTitle = "";
switch (position) {
case 0:
tabHeaderTitle = UC.getString(R.string.waybill_register_package_owner);
break;
case 1:
tabHeaderTitle = UC.getString(R.string.create_new_waybill);
break;
}
return tabHeaderTitle;
}
}
@Override
public void onAttach(Activity activity) {
myContext=(FragmentActivity) activity;
super.onAttach(activity);
}
@Override
public void onResume() {
super.onResume();
}
}
首次切换FragmentMainView()
MainActivity
FragmentMainView()
段FragmentAddNewWayBill()
中的MainActivity
正常工作并显示,但在FragmentMainView()
中启用USERS:
user_id , name , email..
1 Dan a@a.com
2 Ron b@b.com
3 James c@c.com
4 Joe d@d.com
5 Max c@c.com
并再次切换后到viewPager上的FRIENDS:
user_id , friend_id , status
1 2 2
3 1 2
4 3 2
片段不显示,我必须关闭应用程序并再次切换以显示。什么问题?
答案 0 :(得分:0)
使用嵌套片段时,您必须使用getChildFragmentManager()
而不是myContext.getSupportFragmentManager()
。