没有显示的片段中的ViewPager

时间:2015-03-06 14:39:05

标签: android-fragments android-viewpager

我有一个带有一些按钮的父FragmentActivity,每个按钮打开一个子Fragment。其中一个片段(片段A)只包含一个整页视图寻呼机,它显示另外两个片段:B和C.当我第一次打开片段A时,视图寻呼机工作正常,B和C出现。如果我单击按钮打开另一个片段,然后单击片段A,则B和C不再出现。

有什么想法吗?我已经尝试了很多东西

片段A的代码:

public class FragmentA extends Fragment {

FragmentPagerAdapter adapterViewPager;
ViewPager vpPager;
private FragmentActivity myContext;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {

    final View rootView = inflater.inflate(R.layout.fragmentA, container, false);
    vpPager = (ViewPager) rootView.findViewById(R.id.vpPager);
    adapterViewPager = new MyPagerAdapter(myContext.getSupportFragmentManager());
    vpPager.setAdapter(adapterViewPager);
    return rootView;
}

@Override
public void onResume() {
    super.onResume();

    adapterViewPager.notifyDataSetChanged();
}


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 android.support.v4.app.Fragment getItem(int position) {
        switch (position) {
            case 0: 
                return new FragmentB();
            case 1:
                return new FragmentC();

            default:
                return null;
        }
    }

}

@Override
public void onAttach(Activity activity) {
    myContext=(FragmentActivity) activity;
    super.onAttach(activity);
}

来自父FragmentActivity的代码,用于打开片段A的按钮:

public void onClickFragmentA(View v) {
    android.app.FragmentManager fragmentManager = getFragmentManager();
    fragmentManager.beginTransaction()
            .replace(R.id.frame_container, new FragmentA()).commit();

}

0 个答案:

没有答案