android - BottomNavigationView 再次可见后未显示

标签 android

我正在开发 Android 应用程序。有一个名为 HomeActivity 的 Activity ,上面有一个 BottomNavigationView。

还有几个 fragment 。

我从名为 SpotsSearch2Fragment 的 fragment 打开另一个名为 NuevoSpot1Fragment 的 fragment :

 ivNuevoSpot.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NuevoSpot1Fragment nextFrag= new NuevoSpot1Fragment();
                getActivity().getSupportFragmentManager().beginTransaction()
                        .add(R.id.frame_layout, nextFrag, "mapaSpots")
                        .addToBackStack(null)

                        .commit();
            }
        });

在 NuevoSpot1Fragment 中,我从父 Activity 中隐藏了 BottomNavigationView:

((HomeActivity) getActivity()).setNavigationVisibility(false);

工作正常,BottomNavigationView 消失了。

这是 HomeActivity 中的函数,由 fragment 调用以隐藏或显示 BottomNavigationView:

public void setNavigationVisibility(boolean visible) {
        if (bottomNavigationView.isShown() && !visible) {
            bottomNavigationView.setVisibility(View.GONE);
        }
        else if (!bottomNavigationView.isShown() && visible){
            bottomNavigationView.setVisibility(View.VISIBLE);
        }
    }

在 NuevoSpot1Fragment 中有一个按钮应该再次打开 fragment SpotsSearch2Fragment。

btnCerrar.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                getFragmentManager().popBackStack();
            }
        });

因此我在 SpotsSearch2Fragment 中包含了代码:

 @Override
    public void onResume() {
        super.onResume();
        Log.d("JSON", "on resume spot");

        ((HomeActivity) getActivity()).setNavigationVisibility(true);
    }

我的问题是,从 NuevoSpot1Fragment 返回到 SpotsSearch2Fragment,SpotsSearch2Fragment 中的 BottomNavigationView 仍然不可见。

我做错了什么?

编辑

在 BottomNavigationView 应该可见时显示不可见:

enter image description here

最佳答案

替换:

public void setNavigationVisibility(boolean visible) {
    if (bottomNavigationView.isShown() && !visible) {
        bottomNavigationView.setVisibility(View.GONE);
    }
    else if (!bottomNavigationView.isShown() && visible){
        bottomNavigationView.setVisibility(View.VISIBLE);
    }
}

与:

public void setNavigationVisibility(boolean visible) {
    if ((bottomNavigationView.getVisibility() == View.VISIBLE) && !visible) {
        bottomNavigationView.setVisibility(View.GONE);
    }
    else if ((bottomNavigationView.getVisibility() != View.VISIBLE) && visible){
        bottomNavigationView.setVisibility(View.VISIBLE);
    }
}

编辑:我建议您在 fragment 中包含此代码以避免在 fragment 中出现 NullPointerException,并从 mActivity 变量调用 setNavigationVisibility 方法和在 Fragment 中全局声明 mActivity

@Override
public void onAttach(Context context) {
    super.onAttach(context);

    if (context instanceof HomeActivity){
        mActivity = (HomeActivity) context;
    }
}

关于android - BottomNavigationView 再次可见后未显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58354855/

相关文章:

android - 覆盖 osmdroid MapView 的默认平移/移动

android - IllegalStateException:在 ViewPager 的 onSaveInstanceState 之后无法执行此操作

android - 添加新数据时,Firebase Android ChildEventListener 在 onChildAdded() 之前触发 onChildRemoved()

android - 无法将我的 fab 图标设置到我页面的右下角

android - 在 Eclipse 中读取 PNG 元数据 Android UI 设计器时出错

java - 在 SharedPreference.Editor 上调用 putString 时,我的应用程序强制关闭

android - 使用 9-patch png 创建薄分隔线

java - Android base64解码/编码图像到字符串

android - 我可以在 Google Play 商店中隐藏我的应用程序吗?

java - Java 中的 String.Format (.NET) 等价物?