本文介绍了在新片段下方可见上一个片段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有ViewPager的TabLayout. ViewPager具有四个片段F1,F2,F3和F4. F1包含一个FrameLayout,该布局可以包含2个片段F11和F12.最初,我使用以下代码将F11添加到FrameLayout中.

I have a TabLayout with a ViewPager. The ViewPager have four fragments F1, F2, F3 and F4. F1 contains a FrameLayout which can have 2 fragments F11 and F12. Initially I add F11 in the FrameLayout with below code.

Fragment11 fragment11 = new Fragment11();
fragment11.setArguments(getActivity().getIntent().getExtras());
getActivity().getSupportFragmentManager().beginTransaction()
                .add(R.id.fragment_container, fragment11, Constants.FRAGMENT_11)
                .commit();

F11包含一个ListView.当我单击此ListView中的任何项目/行时,F11替换为F12. F12是一个细节片段.

F11 contains a ListView. When I click on any item/row in this ListView then F11 is replaced with F12. F12 is a detail fragment.

Fragment12 fragment12 = new Fragment12();
FragmentTransaction fragmentTransaction = getActivity().getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, fragment12, Constants.FRAGMENT_12)
                    .addToBackStack(null)
                    .commit();

对于正常的应用程序流程,它可以正常工作.现在假设当前我在Fragment12中,并且按下了主页按钮.现在,我打开任何繁重的应用程序(相机或任何其他应用程序)以从内存中删除我的应用程序.现在,我再次启动了我的应用程序.

For normal app flow it works fine. Now suppose currently I am in Fragment12 and I pressed home button. Now I open any heavy application (camera or any other app) to remove my app from memory. Now I started my app again.

现在可以同时看到两个Fragment11和一个Fragment12.当我按回时,Fragment12被删除,现在可以看到两个Fragment11.当我单击Fragment11的ListView行时,顶部的Fragment11替换为Fragment12,但是,底部的Fragment11仍然保留在其中.

Now there are two Fragment11 and one Fragment12 are visible, all at the same time. When I press back Fragment12 is removed and now two Fragment11 are visible. When I click the ListView row of Fragment11 then the top Fragment11 is replaced with Fragment12, however, the bottom Fragment11 remains there.

这就是我想要的:当应用程序从后台进入前景时,则Fragment12应该可见,当我按回时,应该弹出Fragment12以显示Fragment11.

This is what I want:When the app comes to the foreground from background then Fragment12 should be visible and when I press back Fragment12 should be popped to show Fragment11.

我该怎么做?

推荐答案

我知道为时已晚,但是此答案可以帮助其他人.这不是一个适当的解决方案,但对我有用.因此,将android:clickable="true"android:background="?android:attr/colorBackground"放在片段的根视图中.希望这会工作.

I know it's too late but this answer can help others. it's not a proper solution but it's working for me. So put android:clickable="true" and android:background="?android:attr/colorBackground" in the root view of your fragment. hope this will work.

这篇关于在新片段下方可见上一个片段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

07-18 19:45