getChildFragmentManager() and viewpager

ぐ巨炮叔叔 提交于 2019-12-06 05:56:21

问题


I have the same problem as Navigating back to FragmentPagerAdapter -> fragments are empty but would like some clarification on the solution of using getChildFragmentManager().

This solution uses getChildFragmentManager(), the manager for fragments inside this Fragment(OuterFragment, which has the viewpager). InnerFragment is a page inside OuterFragment. When someone clicks the listview in InnerFragment, I want InnerFragment2 to replace InnerFragment. What do the xml and fragment managers look like?

fragment_outer.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/vpPager"
    android:layout_height="wrap_content"
    android:layout_width="match_parent">

    <android.support.v4.view.PagerTabStrip
        android:id="@+id/pager_header"
        android:layout_gravity="top"
        android:layout_height="wrap_content"
        android:layout_width="match_parent"
        android:paddingBottom="4dp"
        android:paddingTop="4dp" />

    <FrameLayout
        android:id="@+id/inner_content"
        android:layout_width="match_parent"
        android:layout_height="match_parent" />

</android.support.v4.view.ViewPager>

OuterFragment.java:

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

View v = inflater.inflate(R.layout.fragment_outer, container, false);
vpPager = (ViewPager) v.findViewById(R.id.vpPager);

FragmentManager cfManager = getChildFragmentManager();
adapterViewPager = new MyPagerAdapter(cfManager);

vpPager.setAdapter(adapterViewPager);
return v;
}

public static class MyPagerAdapter extends FragmentPagerAdapter {

public MyPagerAdapter(FragmentManager fragmentManager) {
    super(fragmentManager);
}

@Override
public int getCount() {
    return 2;
}

// Returns the fragment to display for that page
@Override
public Fragment getItem(int position) {
    switch (position) {
        case 0:
            return InnerFragment.newInstance(false);
        case 1:
            return InnerFragment.newInstance(true);
        default:
            return null;
    }
}

InnerFragment shows up as a fragment inside OuterFragment, which has the viewpager. InnerFragment has a listview that when clicked, should replace InnerFragment with InnerFragment2.

InnerFragment.java:

FragmentManager fm = getChildFragmentManager();
fm = fm.beginTransaction();
ft.addToBackStack(null);
ft = ft.replace(R.id.inner_content,
                InnerFragment2.newInstance(someArg));
ft.commit();

This throws an error saying InnerFragment2 does not recognize view R.id.inner_content.

Not only should InnerFragment2 replace InnerFragment, but when InnerFragment2 is shown and it's button is clicked, I want InnerFragment2 replaced with InnerFragment3. I basically want to use inner_content as an inner container and keep adding fragments to a backstack for appropriate back behavior.

Do I getChildFragmentManager() or getFragmentManager() each time I add to my fragment backstack, what does the xml pattern look like, because having one framelayout in my outerfragment isn't doing it.


回答1:


public void setupViewPager(ViewPager viewPager) {

    FragmentManager cfManager=getChildFragmentManager();
    viewPagerAdapter = new ViewPagerAdapter(cfManager);

   // viewPagerAdapter = new ViewPagerAdapter(getActivity().getSupportFragmentManager());
    viewPagerAdapter.addFragment(new EntertainmentWallpaperFragment(), getString(R.string.title_entertainment_wallpapers));
    viewPagerAdapter.addFragment(new EntertainmentMusicFragment(), getString(R.string.title_entertainment_music));
    viewPagerAdapter.addFragment(new EntertainmentGamesFragment(), getString(R.string.title_entertainment_games));
    viewPagerAdapter.addFragment(new EntertainmentEBooksFragment(), getString(R.string.title_entertainment_ebooks));
    viewPagerAdapter.addFragment(new EntertainmentAppsFragment(), getString(R.string.title_entertainment_apps));
    viewPager.setAdapter(viewPagerAdapter);
}

I hope this helps. I had the same problem where the fragments apear but once you go back they dissapear and find an empty fragent. Commnented the line that I had first implemented which after updating the fragment manager I was able to enjoy free smooth scroll.



来源:https://stackoverflow.com/questions/26172449/getchildfragmentmanager-and-viewpager

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!