简体   繁体   English

Android 导航底部片段重叠

[英]Android Navigation Bottom Fragments overlapping

I am using a navigation bottom with 4 items in my app, so I have 4 fragments.我在我的应用程序中使用了带有 4 个项目的导航底部,所以我有 4 个片段。 the first fragment(home page) contains a recyclerView and other fragments don't contain any recyclerView.第一个片段(主页)包含一个 recyclerView,其他片段不包含任何 recyclerView。

The problem is here;问题就在这里;
when I navigate to other fragments I can see the recycler view in the background.当我导航到其他片段时,我可以在后台看到回收站视图。 and when I navigatie back to the first fragment there is another recycler view under the original one!当我导航回第一个片段时,在原始视图下有另一个回收器视图!

I have used this: fm.beginTransaction().hide(active).show(fragment2).commit();我用过这个: fm.beginTransaction().hide(active).show(fragment2).commit();
but the hide() method doesn't work.但是 hide() 方法不起作用。


Here is the related parts of my code:这是我的代码的相关部分:

I have globally defined these我已经在全球范围内定义了这些

final Fragment fragment1 = new HomeFragment();
final Fragment fragment2 = new AddFragment();
final Fragment fragment3 = new CalendarFragment();
final Fragment fragment4 = new ProfileFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;

then然后

In the onCreate:在 onCreate 中:

fm.beginTransaction().add(R.id.nav_host_fragment, fragment4, "4").hide(fragment4).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.nav_host_fragment, fragment1, "1").commit();

and at last最后

the navigation item listener:导航项侦听器:

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
            = new BottomNavigationView.OnNavigationItemSelectedListener() {
        @Override
        public boolean onNavigationItemSelected(@NonNull MenuItem item) {
            switch (item.getItemId()) {
                case R.id.navigation_home:
                    if (active == fragment1)
                        return false;
                    fm.beginTransaction().hide(active).show(fragment1).commit();
                    active = fragment1;
                    return true;
                case R.id.navigation_add:
                    if (active == fragment2)
                        return false;
                    fm.beginTransaction().hide(active).show(fragment2).commit();
                    active = fragment2;
                    return true;
                case R.id.navigation_calendar:
                    if (active == fragment3)
                        return false;
                    fm.beginTransaction().hide(active).show(fragment3).commit();
                    active = fragment3;
                    return true;
                case R.id.navigation_profile:
                    if (active == fragment4)
                        return false;
                    fm.beginTransaction().hide(active).show(fragment4).commit();
                    active = fragment4;
                    return true;
            }
            return false;
        }
    };

I have used navGraph in my fragment in the XML file previously and I've forgotten to delete the navGraph, so it was showing the first fragment in the navGraph in the background.我以前在 XML 文件的片段中使用过 navGraph,但我忘记删除 navGraph,所以它在后台显示 navGraph 中的第一个片段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM