Android导航底部片段重叠 [英] Android Navigation Bottom Fragments overlapping

查看:210
本文介绍了Android导航底部片段重叠的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我的应用程序中使用的导航栏包含4个项目,因此我有4个片段. 第一个片段(主页)包含一个recyclerView,其他片段不包含任何recyclerView.

I am using a navigation bottom with 4 items in my app, so I have 4 fragments. the first fragment(home page) contains a recyclerView and other fragments don't contain any 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!

我用了这个: fm.beginTransaction().hide(active).show(fragment2).commit();
但是hide()方法不起作用.

I have used this : fm.beginTransaction().hide(active).show(fragment2).commit();
but the hide() method doesn't work.

这是我代码的相关部分:

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;

然后

在onCreate中:

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

最后

导航项侦听器:

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

推荐答案

我以前在XML文件的片段中使用过navGraph,但我忘记删除了navGraph,因此它显示了navGraph中的第一个片段背景.

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.

这篇关于Android导航底部片段重叠的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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