本文介绍了片段BackStack.即使弹出,片段也会重叠.的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是要正确管理后退堆栈,以便弹出(或删除)以前的片段.问题在于它们的重叠.

The problem is to manage properly back stack so the previous fragment is poped out (or deleted). The problem is in their overlapping..

程序结构如下:

  • 滑动菜单,每个部分包含3个片段:CatalogFragmentNewsFragmentBlogFragment;
  • 每个片段都是一个带有项目的listView(从JSON解析);
  • CatalogFragment的项目上单击,我需要将此CatalogFragment替换为LessonsFragment,这也是列表.
  • sliding menu with 3 fragments for each section: CatalogFragment, NewsFragment, BlogFragment;
  • each fragment is a listView with items (parsed from JSON);
  • on CatalogFragment's item click I need to replace this CatalogFragment with LessonsFragment, which is list also.

p.s.项目是俄语,但我认为您可以理解

这是动态添加这些片段的方式:

This is how these fragments are added (dynamically):

    @Override
public void onNavigationDrawerItemSelected(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();
//        fragmentManager.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE); //this also doesn't work

    switch (position+1) {
        case 1:
            //getSupportFragmentManager().popBackStack(); // this doesnt work
            fragmentManager.beginTransaction().replace(R.id.container,
                    CatalogFragment.newInstance(position + 1)).addToBackStack("catalog").commit();

            break;
        case 2:
            fragmentManager.beginTransaction().replace(R.id.container,
                    NewsFragment.newInstance(position + 1)).addToBackStack("news").commit();

            break;
        case 3:
            fragmentManager.beginTransaction().replace(R.id.container,
                    BlogFragment.newInstance(position + 1)).addToBackStack("blog").commit();
            break;
    }
}

这就是我如何通过接口的onCatalogFragmentInteraction方法中的新片段替换片段:

That's how I replace fragment with new one in onCatalogFragmentInteraction method from interface:

    /** Methods for interaction with list items*/
@Override
public void onCatalogFragmentInteraction(String id){
    //pop previous
    getSupportFragmentManager().popBackStack("catalog", FragmentManager.POP_BACK_STACK_INCLUSIVE);
    //add new
    FragmentTransaction fragmentTransaction = getFragmentManager().beginTransaction();
    fragmentTransaction.replace(R.id.container, LessonsFragment.newInstance());
    fragmentTransaction.addToBackStack("lessons");
    fragmentTransaction.commit();
}

...而且效果很好:

...and that works fine:

但是当我从滑动菜单移回片段时,片段重叠了.

But when I move back to fragment from slididng menu fragments overlap.

我相信问题出在正确的BackStack管理上,但我无法弄清楚自己做错了什么.另一个建议是,在这种情况下,我需要更好地使用add/replace.

I belive the problem lies in proper BackStack management but I can't figure out what I did wrong. Another suggestion is that I need to use add/replace somehow better in that case.

我已经尝试按名称从堆栈中删除.也许需要通过ID删除它们?

I have tried already deleting by name from stack. Maybe needed to delete them by ID?

P.S.告诉是否需要更多代码.提前致谢.

P.S. Tell if some more code needed.Thanks in advance.

推荐答案

我认为您的所有片段都具有透明背景,或者您未设置任何内容(因此默认设置是透明的).因此,当您在另一个片段上方添加/替换一个片段时,您可以看到以下片段.因此,请尝试为每个片段布局设置背景色.

I think your all fragments have transparent background or you did not set anything(so default is transparent). So the when you add/replace a fragment above another fragment the below fragment is visible to you. So try to set the background color for every fragment layout.

这篇关于片段BackStack.即使弹出,片段也会重叠.的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-31 03:56