• <small id='MUCfI'></small><noframes id='MUCfI'>

    <i id='MUCfI'><tr id='MUCfI'><dt id='MUCfI'><q id='MUCfI'><span id='MUCfI'><b id='MUCfI'><form id='MUCfI'><ins id='MUCfI'></ins><ul id='MUCfI'></ul><sub id='MUCfI'></sub></form><legend id='MUCfI'></legend><bdo id='MUCfI'><pre id='MUCfI'><center id='MUCfI'></center></pre></bdo></b><th id='MUCfI'></th></span></q></dt></tr></i><div id='MUCfI'><tfoot id='MUCfI'></tfoot><dl id='MUCfI'><fieldset id='MUCfI'></fieldset></dl></div>

      <tfoot id='MUCfI'></tfoot>
        <bdo id='MUCfI'></bdo><ul id='MUCfI'></ul>
    1. <legend id='MUCfI'><style id='MUCfI'><dir id='MUCfI'><q id='MUCfI'></q></dir></style></legend>

        FragmentManager 在使用 setOffscreenPageLimit() 方法后已经在执行事务

        时间:2023-10-04
          <bdo id='FvJ4K'></bdo><ul id='FvJ4K'></ul>
            <tbody id='FvJ4K'></tbody>
          <i id='FvJ4K'><tr id='FvJ4K'><dt id='FvJ4K'><q id='FvJ4K'><span id='FvJ4K'><b id='FvJ4K'><form id='FvJ4K'><ins id='FvJ4K'></ins><ul id='FvJ4K'></ul><sub id='FvJ4K'></sub></form><legend id='FvJ4K'></legend><bdo id='FvJ4K'><pre id='FvJ4K'><center id='FvJ4K'></center></pre></bdo></b><th id='FvJ4K'></th></span></q></dt></tr></i><div id='FvJ4K'><tfoot id='FvJ4K'></tfoot><dl id='FvJ4K'><fieldset id='FvJ4K'></fieldset></dl></div>

            1. <legend id='FvJ4K'><style id='FvJ4K'><dir id='FvJ4K'><q id='FvJ4K'></q></dir></style></legend>
                • <small id='FvJ4K'></small><noframes id='FvJ4K'>

                  <tfoot id='FvJ4K'></tfoot>
                  本文介绍了FragmentManager 在使用 setOffscreenPageLimit() 方法后已经在执行事务的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个带有许多片段的导航抽屉,其中一个是带有两个 recyclerview 片段的 tablayout,RecyclerView 项目在导航抽屉片段之间切换后消失,我找到了一个解决方案,我需要使用 setOffscreenPageLimit() 方法ViewPager 但它给了我问题标题中提到的错误

                  i have a navigation drawer with a number of fragments and one of them is a tablayout with two recyclerview fragments , the RecyclerView items disappear after switching between navigation drawer fragments , i found a solution that i need to use method setOffscreenPageLimit() with the ViewPager but it gives me the bug that is mentioned in the question title

                  这是我的日志

                  java.lang.IllegalStateException: FragmentManager is already executing transactions
                                                                                                 at android.support.v4.app.FragmentManagerImpl.execSingleAction(FragmentManager.java:1626)
                                                                                                 at android.support.v4.app.BackStackRecord.commitNowAllowingStateLoss(BackStackRecord.java:679)
                                                                                                 at android.support.v4.app.FragmentPagerAdapter.finishUpdate(FragmentPagerAdapter.java:143)
                                                                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1240)
                                                                                                 at android.support.v4.view.ViewPager.populate(ViewPager.java:1088)
                                                                                                 at android.support.v4.view.ViewPager.setOffscreenPageLimit(ViewPager.java:852)
                                                                                                 at com.amir.ahmed.EELUStudentUnion.SelectionFragment.setupViewPager(SelectionFragment.java:59)
                                                                                                 at com.amir.ahmed.EELUStudentUnion.SelectionFragment.onActivityCreated(SelectionFragment.java:40)
                                                                                                 at android.support.v4.app.Fragment.performActivityCreated(Fragment.java:2089)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1133)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.moveToState(FragmentManager.java:1290)
                                                                                                 at android.support.v4.app.BackStackRecord.run(BackStackRecord.java:801)
                                                                                                 at android.support.v4.app.FragmentManagerImpl.execPendingActions(FragmentManager.java:1677)
                                                                                                 at android.support.v4.app.FragmentManagerImpl$1.run(FragmentManager.java:536)
                                                                                                 at android.os.Handler.handleCallback(Handler.java:739)
                                                                                                 at android.os.Handler.dispatchMessage(Handler.java:95)
                                                                                                 at android.os.Looper.loop(Looper.java:158)
                                                                                                 at android.app.ActivityThread.main(ActivityThread.java:7225)
                                                                                                 at java.lang.reflect.Method.invoke(Native Method)
                                                                                                 at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1230)
                                                                                                 at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1120)
                  

                  那是选项卡布局片段代码

                  an that is the tab layout fragment code

                  @Override
                  public void onActivityCreated(@Nullable Bundle savedInstanceState) {
                      super.onActivityCreated(savedInstanceState);
                  
                      viewPager = (ViewPager) getActivity().findViewById(R.id.viewpager);
                      setupViewPager(viewPager);
                  
                      viewPager.setOffscreenPageLimit(2);
                  
                      tabLayout = (TabLayout) getActivity().findViewById(R.id.tabs);
                      tabLayout.setupWithViewPager(viewPager);
                      setupTabIcons();
                  }
                  
                  
                  
                  private void setupViewPager(ViewPager viewPager) {
                      ViewPagerAdapter adapter = new ViewPagerAdapter(getFragmentManager());
                      adapter.addFrag(new Courses(), "IT");
                      adapter.addFrag(new CoursesBussiness(), "Business");
                      viewPager.setAdapter(adapter);
                  }
                  

                  推荐答案

                  我遇到了和你类似的问题,经过几个小时的研究,我发现了这个问题.如果您的 ViewPager 是在 Fragment 内部创建的,则需要对 FragmentPagerAdapter 使用 getChildFragmentManager(),而不是 getFragmentManager().

                  I encountered the similar issue as you, after several hours research, I found the issue. If your ViewPager is created inside the Fragment, you need to use getChildFragmentManager() for the FragmentPagerAdapter, rather than getFragmentManager().

                  所以你的 setupViewPager() 方法应该是这样的.

                  So your setupViewPager() method should look like this.

                  private void setupViewPager(ViewPager viewPager) {
                      ViewPagerAdapter adapter = new ViewPagerAdapter(getChildFragmentManager());
                      adapter.addFrag(new Courses(), "IT");
                      adapter.addFrag(new CoursesBussiness(), "Business");
                      viewPager.setAdapter(adapter);
                  }
                  

                  问题的根本原因是您在 Fragment 中嵌入了 Fragments,这就是它会抛出异常的原因.

                  The root cause of the issue is you embeded Fragments inside the Fragment, that's why it will throw the exception.

                  java.lang.IllegalStateException: FragmentManager is already executing transactions
                  

                  根据 Google 的文档:

                  According to the Google's docs:

                  您现在可以在片段中嵌入片段.这对于一个有用的您希望在其中放置动态和可重用的各种情况将 UI 组件转换为本身是动态的 UI 组件可重复使用的.例如,如果您使用 ViewPager 创建片段左右滑动并占用大部分屏幕空间,你现在可以将片段插入每个片段页面.嵌套一个片段,只需在您所在的 Fragment 上调用 getChildFragmentManager()想添加一个片段.这将返回一个 FragmentManager,您可以像平常一样从顶级活动中使用来创建分片交易.

                  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.

                  这篇关于FragmentManager 在使用 setOffscreenPageLimit() 方法后已经在执行事务的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  上一篇:从另一个 Fragment 更新 ViewPager 中的 TextView 下一篇:Fragment 和 FragmentStatePagerAdapter 中带有 ViewPager 的片段导致异常(带

                  相关文章

                  1. <i id='W7mcn'><tr id='W7mcn'><dt id='W7mcn'><q id='W7mcn'><span id='W7mcn'><b id='W7mcn'><form id='W7mcn'><ins id='W7mcn'></ins><ul id='W7mcn'></ul><sub id='W7mcn'></sub></form><legend id='W7mcn'></legend><bdo id='W7mcn'><pre id='W7mcn'><center id='W7mcn'></center></pre></bdo></b><th id='W7mcn'></th></span></q></dt></tr></i><div id='W7mcn'><tfoot id='W7mcn'></tfoot><dl id='W7mcn'><fieldset id='W7mcn'></fieldset></dl></div>
                    <tfoot id='W7mcn'></tfoot>

                    <small id='W7mcn'></small><noframes id='W7mcn'>

                      <legend id='W7mcn'><style id='W7mcn'><dir id='W7mcn'><q id='W7mcn'></q></dir></style></legend>

                      • <bdo id='W7mcn'></bdo><ul id='W7mcn'></ul>