android – 具有嵌套片段的ViewPager?

前端之家收集整理的这篇文章主要介绍了android – 具有嵌套片段的ViewPager?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
我的问题

根据谷歌的文档:

You can now embed fragments inside fragments. This is useful for a
variety of situations in which you want to place dynamic and re-usable
UI components into a UI component that is itself dynamic and
re-usable. For example,if you use ViewPager to create fragments that
swipe left and right and consume a majority of the screen space,you
can now insert fragments into each fragment page. To nest a fragment,
simply call getChildFragmentManager() on the Fragment in which you
want to add a fragment. This returns a FragmentManager that you can
use like you normally do from the top-level activity to create
fragment transactions. For example,here’s some code that adds a
fragment from within an existing Fragment class:

Fragment videoFragment = new VideoPlayerFragment();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.add(R.id.video_fragment,videoFragment).commit();

所以我创建了自己的PageFragment.而我的PageFragmentAdapter包含3页PageFragment.
我还有六个片段:Fragment1,Fragment2,Fragment3,FragmentA,FragmentB,FragmentC.
在我的MainActivity中,我初始化了我的六个片段,我的ViewPager和它的PageFragmentAdapter(它自己初始化了三个PageFragment).

然后我使用这个方法将Fragment1,Fragment3附加到PageFragmentAdapter的每个页面(到每个PageFragment):

PageFragmentAdapter tPageFragmentAdapter = new PageFragmentAdapter(getSupportFragmentManager());
tPageFragmentAdapter.setFirstPage(tFragment1);
tPageFragmentAdapter.setSecondPage(tFragment2);
tPageFragmentAdapter.setThirdPage(tFragment3);

我可以(通过PageFragmentAdapter中的方法)重新获取片段并将“嵌套片段”放入我的PageFragment(当然在onCreate()中),如:

getChildFragmentManager().beginTransaction()
.add(R.id.framelayout_history,mFragment)
.addToBackStack(null)
.commit();

注意:我使用PageFragment.setNestedFragment()在其中设置mFragment.

而且效果很棒!
它将帮助我(但这个故事不是关于它)简单地替换ViewPager中的片段,如:

tPageFragmentAdapter.replaceFirstPage(tFragmentA);

但我有一个很大的问题.
再次谷歌的文档:

Implementation of PagerAdapter that represents each page as a Fragment
that is persistently kept in the fragment manager as long as the user
can return to the page. This version of the pager is best for use when
there are a handful of typically more static fragments to be paged
through,such as a set of tabs. The fragment of each page the user
visits will be kept in memory,though its view hierarchy may be
destroyed when not visible. This can result in using a significant
amount of memory since fragment instances can hold on to an arbitrary
amount of state.

正如您所看到的,PageFragmentAdapter可以销毁我的每个PageFragment,当然还有它的嵌套片段.当它将被重新创建时,它将具有NULL而不是mFragment值.
我试图通过getChildFragmentManager().putFragment()保存它,然后通过getChildFragmentManager().getFragment()获取它.但它不起作用.

所以我的问题是:如何将我的嵌套片段保存在我的父片段中?可能吗?

我非常感谢你的帮助.亚历克斯.附:真的很抱歉我的英文:)

编辑

Thx @AndroidBegin.com的答案!

This seems to be a bug in the newly added support for nested
fragments. Basically,the child FragmentManager ends up with a broken
internal state when it is detached from the activity. A short-term
workaround that fixed it for me is to add the following to onDetach()
of every Fragment which you call getChildFragmentManager() on:

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

    try {
        Field childFragmentManager = Fragment.class.getDeclaredField("mChildFragmentManager");
        childFragmentManager.setAccessible(true);
        childFragmentManager.set(this,null);

    } catch (NoSuchFieldException e) {
        throw new RuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new RuntimeException(e);
    }
}

解决方法

在你的片段上尝试这个.
public class Fragment2 extends SherlockFragment {

    @Override
    public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.viewpager_main,container,false);
        // Locate the ViewPager in viewpager_main.xml
        ViewPager mViewPager = (ViewPager) view.findViewById(R.id.viewPager);
        // Set the ViewPagerAdapter into ViewPager
        mViewPager.setAdapter(new ViewPagerAdapter(getChildFragmentManager()));
        return view;
    }

    @Override
    public void onDetach() {
        super.onDetach();
        try {
            Field childFragmentManager = Fragment.class
                    .getDeclaredField("mChildFragmentManager");
            childFragmentManager.setAccessible(true);
            childFragmentManager.set(this,null);
        } catch (NoSuchFieldException e) {
            throw new RuntimeException(e);
        } catch (IllegalAccessException e) {
            throw new RuntimeException(e);
        }
    }
}

资料来源:http://www.androidbegin.com/tutorial/actionbarsherlock-side-menu-navigation-nested-viewpager-fragment-tabs-tutorial/

原文链接:https://www.f2er.com/android/308870.html

猜你在找的Android相关文章