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

如何解决底部导航-使其中一项打开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();
    }

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

解决方法

我已经可以做到,只需将<fragment>标记周围CoordinatorLayout如下所示

activity_main.xml

<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <androidx.coordinatorlayout.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1">

        <fragment
            android:id="@+id/nav_host_fragment"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:defaultNavHost="true"
            app:navGraph="@navigation/nav_graph"
            android:fillViewport="true"
            app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

    </androidx.coordinatorlayout.widget.CoordinatorLayout>

    <com.google.android.material.bottomnavigation.BottomNavigationView
        android:id="@+id/bottom_nav_view"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:menu="@menu/bottom_navigation_menu"
        app:labelVisibilityMode="labeled"
        />
</LinearLayout>

MyBottomSheet

class BottomSheetFragment: BottomSheetDialogFragment(),View.OnClickListener {

    override fun onCreateDialog(savedInstanceState: Bundle?): Dialog = BottomSheetDialog(requireContext(),theme)

    override fun onCreateView(inflater: LayoutInflater,container: ViewGroup?,savedInstanceState: Bundle?): View? {
        return inflater.inflate(R.layout.fragment_bottom_sheet,container,false)
    }
}

或者@Naveen Rao的答案

findViewById(R.id.nav_bottom_view).setOnNavigationItemSelectedListener {
    // This is to avoid going to startDestination of opening of the BottomSheet
    navController.navigate(it.itemId)
    true
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?