ViewPager使用嵌套片段? [英] ViewPager with Nested Fragments?

查看:196
本文介绍了ViewPager使用嵌套片段?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题

按照谷歌的文档:

  

您现在可以内部碎片嵌入片段。这为有用   各种各样的要在其中放置动态的,可重复使用的情况下,   UI组件到UI组件本身是动态的,   重复使用的。例如,如果您使用ViewPager创建片段    刷卡左,右和消耗了大部分屏幕空间,你    现在可以插入片段到每个片段的页面。要嵌套片段,   只需调用getChildFragmentManager()的片段,其中您   要添加一个片段。这会返​​回一个FragmentManager,你可以   使用像你这样的顶级活动通常做创建   片段交易。例如,这里的一些code,增加了   从现有的片段类中的片段:

 片段videoFragment =新VideoPlayerFragment();
FragmentTransaction交易= getChildFragmentManager()的BeginTransaction()。
transaction.add(R.id.video_fragment,videoFragment).commit();
 

所以,我创建了自己的 PageFragment 。而我的 PageFragmentAdapter 由3页 PageFragment 的。 我也有六个片段:片段1 Fragment2 Fragment3 碎裂 FragmentB FragmentC 。 在我的 MainActivity 我初始化参加六片段,我的 ViewPager PageFragmentAdapter (其中初始化3个 PageFragment 本身)。

然后,我用这个方法来附上我的片段1 Fragment2 Fragment3 每个页面(每个 PageFragment )的 PageFragmentAdapter

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

我可以(在 PageFragmentAdapter 通过方法)片段recive,使嵌套的片段为我(的 PageFragment 当然,在的onCreate()),如:

  getChildFragmentManager()的BeginTransaction()
。新增(R.id.framelayout_history,mFragment)
.addToBackStack(空)
。承诺();
 

注:我用 PageFragment.setNestedFragment()设置 mFragment

和它的伟大工程! 这会帮助我(但这个故事是不是这件事),以简单地ViewPager取代片段如下:

  tPageFragmentAdapter.replaceFirstPage(tFragmentA);
 

不过,我有一个巨大的问题。 此外谷歌的文档:

  

PagerAdapter的实现,再presents每一页作为一个片段   只要是持续保持在片段管理器作为用户   可以返回到页。这个版本的寻呼机是最适合使用的时候   也有少数通常比较静态的片段进行分页   通过,比如一组选项卡。 每个页面的片段用户    访问将被保存在内存中,虽然它的视图层次结构可能是    破坏时不可见。这可能会导致使用显著   自片段实例存储量可以容纳到一个任意   量的状态。

正如你可以看到 PageFragmentAdapter 可以摧毁我的每一个 PageFragment ,当然其嵌套的片段。而当它将重新将有一个NULL,而不是 mFragment 值。 我想通过 getChildFragmentManager()保存。putFragment(),然后把它通过 getChildFragmentManager()。getFragment()。不过,这并不奏效。

问题

所以我的问题是:如何拯救我的嵌套的片段在我父母的片段?这可能吗?

我将不胜AP preciate对您有所帮助。亚历克斯。附:真的很抱歉,我的英语:)

修改

THX @ AndroidBegin.com的答案!

  

这似乎是新添加的支持,一个错误的嵌套   片段。基本上,孩子FragmentManager最终以破   当它从该活动分离的内部状态。一个短期   解决方法是修复这对我来说是以下添加到onDetach()   每一个你调用getChildFragmentManager()的片段:

  @覆盖
公共无效onDetach(){
    super.onDetach();

    尝试 {
        现场childFragmentManager = Fragment.class.getDeclaredField(mChildFragmentManager);
        childFragmentManager.setAccessible(真正的);
        childFragmentManager.set(本,NULL);

    }赶上(NoSuchFieldException E){
        抛出新的RuntimeException(E);
    }赶上(IllegalAccessException E){
        抛出新的RuntimeException(E);
    }
}
 

解决方案

试试这个在您的片段。

 公共类Fragment2扩展SherlockFragment {

    @覆盖
    公共查看onCreateView(LayoutInflater充气,容器的ViewGroup,
            捆绑savedInstanceState){
        查看查看= inflater.inflate(R.layout.viewpager_main,集装箱,假);
        //找到ViewPager在viewpager_main.xml
        ViewPager mViewPager =(ViewPager)view.findViewById(R.id.viewPager);
        //设置ViewPagerAdapter到ViewPager
        mViewPager.setAdapter(新ViewPagerAdapter(getChildFragmentManager()));
        返回查看;
    }

    @覆盖
    公共无效onDetach(){
        super.onDetach();
        尝试 {
            现场childFragmentManager = Fragment.class
                    .getDeclaredField(mChildFragmentManager);
            childFragmentManager.setAccessible(真正的);
            childFragmentManager.set(本,NULL);
        }赶上(NoSuchFieldException E){
            抛出新的RuntimeException(E);
        }赶上(IllegalAccessException E){
            抛出新的RuntimeException(E);
        }
    }
}
 

来源:<一href="http://www.androidbegin.com/tutorial/actionbarsherlock-side-menu-navigation-nested-viewpager-fragment-tabs-tutorial/">http://www.androidbegin.com/tutorial/actionbarsherlock-side-menu-navigation-nested-viewpager-fragment-tabs-tutorial/

My problem

According to the Google's docs:

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();

So I created my own PageFragment. And my PageFragmentAdapterconsist of 3 pages of PageFragment. I also have six fragments: Fragment1, Fragment2, Fragment3, FragmentA, FragmentB, FragmentC. In my MainActivity I initialize my six fragments, my ViewPager and its PageFragmentAdapter (which initialize three PageFragment for itself).

Then I use this method to attach my Fragment1, Fragment2, Fragment3 to each page (to each PageFragment) of PageFragmentAdapter:

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

I can recive (via methods in PageFragmentAdapter) Fragments and make "nested fragment" into my PageFragment (of course in onCreate()) like:

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

Note: I use PageFragment.setNestedFragment() to set mFragment in it.

And it works great! It will help me (but this story is not about it) to simply replace fragments in ViewPager like:

tPageFragmentAdapter.replaceFirstPage(tFragmentA);

But I have one huge problem. Again Google's docs:

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.

As you can see PageFragmentAdapter can destroy each my PageFragment and of course its nested fragment. And when it will be recreated it will have a NULL instead mFragment value. I am trying to save it via getChildFragmentManager().putFragment() and then get it by getChildFragmentManager().getFragment(). But it doesn't worked.

Question

So my question is: How to save my nested fragment in my parent fragment? Is it possible?

I would greatly appreciate for your help. Alex. P.S. Really sorry for my English:)

EDIT

Thx @AndroidBegin.com for the answer!

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);
    }
}

解决方案

Try this on your fragment.

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);
        }
    }
}

Source : http://www.androidbegin.com/tutorial/actionbarsherlock-side-menu-navigation-nested-viewpager-fragment-tabs-tutorial/

这篇关于ViewPager使用嵌套片段?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆