底部导航-使其中一项打开BottomSheet

问题描述

我只想让我的“更多”菜单点打开BottomSheet

enter image description here

enter image description here


我正在将新的Android导航组件与导航图一起使用,但是我不知道该在哪里覆盖行为。

我的menu.xml看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">

    <item
        android:id="@+id/navigation_home_screen"
        android:icon="@drawable/ic_add_white_24dp"
        android:title="@string/title_home" />

    <item
        android:id="@+id/navigation_sas"
        android:icon="@drawable/ic_check_white_24dp"
        android:title="@string/title_activity_sa" />

    <item
        android:id="@+id/navigation_profile"
        android:icon="@drawable/ic_close_white_24dp"
        android:title="@string/title_profile" />

    <item
        android:id="@+id/navigation_more_menu"
        android:icon="@drawable/ic_more_horiz_24"
        android:title="@string/title_more_menu" />
</menu>

我的nav-graph

<?xml version="1.0" encoding="utf-8"?>
<navigation xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/mobile_navigation"
    app:startDestination="@id/navigation_home_screen">

    <fragment
        android:id="@+id/navigation_home_screen"
        android:name="com.dev.solidmind.ui.HomeScreenFragment"
        android:label="@string/title_home"
        tools:layout="@layout/fragment_home_screen" />

    <fragment
        android:id="@+id/navigation_sas"
        android:name="com.dev.solidmind.ui.SA_Fragment"
        android:label="@string/title_activity_sa"
        tools:layout="@layout/fragment_sa" />

    <fragment
        android:id="@+id/navigation_profile"
        android:name="com.dev.solidmind.ui.ProfileFragment"
        android:label="@string/title_profile"
        tools:layout="@layout/fragment_profile" />

    <dialog
        android:id="@+id/navigation_more_menu"
        android:label="@string/title_more_menu" />
</navigation>

如您所见,我尝试将更多菜单BottomSheet作为对话框或其他项目添加,但是没有任何效果。

我在导航组件的设置方法中添加了所有内容,并认为我可以覆盖addOnDestinationChangedListener中的行为,但是由于它试图导航到该菜单点而引发了错误:

@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main2);
        BottomNavigationView bottomNavView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
                R.id.navigation_home_screen,R.id.navigation_sas,R.id.navigation_profile,R.id.navigation_more_menu)
                .build();
        NavController navController = Navigation.findNavController(this,R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this,navController,appBarConfiguration);
        NavigationUI.setupWithNavController(bottomNavView,navController);

        navController.addOnDestinationChangedListener((controller,destination,arguments) -> {
            if (destination.getId() == R.id.navigation_home_screen) {
                Objects.requireNonNull(getSupportActionBar()).hide();
            } else if (destination.getId() == R.id.navigation_more_menu) {
                openBottomSheet();
            } else {
                Objects.requireNonNull(getSupportActionBar()).show();
            }
        });
    }

openBottomSheet方法:

public void openBottomSheet() {
        View dialogView = getLayoutInflater().inflate(R.layout.main_bottom_sheet_menu,findViewById(R.id.main_root_layout),false);
        BottomSheetDialog dialog = new BottomSheetDialog(this);

        if (paidContentUnlocked)
            adaptBottomSheetToPaidStatus(dialogView);

        dialog.setContentView(dialogView);
        dialog.show();
    }

我可以阻止导航而只打开菜单吗?
无需回到手动管理带有事务等的片段...

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)