android - 如何从 fragment 中拦截 MainActivity 的 BottomNavigationView 菜单点击

标签 android kotlin bottomnavigationview android-jetpack android-architecture-navigation

我的应用程序像向导一样工作。我有一个 Activity 可以集中所有 fragment 。每个 fragment 中都有一个带有“下一步”按钮的 BottomNavigationView 来驱动向导。

当我想下一步时,我从 BottomNavMenu 调用“action_next”,它导航到下一个 fragment 。

但是当用户在该 fragment 的上下文中按下下一步按钮时,我需要执行一些操作(比如存储输入的数据)。此外,如果用户输入的数据有任何问题,我需要取消导航。

起初尝试在我的 fragment 中这样做:

val controller = NavHostFragment.findNavController(this)

        controller.addOnNavigatedListener { controller, navDestination: NavDestination ->

            when (navDestination.id) {
                R.id.destination_setup_tournment -> {
                    proceedNavigation(controller, navDestination)

                }

            }
        }


private fun proceedNavigation(controller: NavController, navDestination: NavDestination) {
    val teams = teamAdapter.getItems()
    if (validateSubmit(teams)){
        presenter.saveTeams(teams)
    }else{
        //how to cancel navigation and stay on this fragment?
    }
}

但它看起来不太好,甚至对我来说都不正确,如果出现问题,我不知道如何取消导航。

App相关文件如下:

主要 Activity :

<androidx.appcompat.widget.Toolbar
    android:id="@+id/toolbar"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="?colorPrimary"
    android:theme="@style/ToolbarTheme" />

<fragment
    android:id="@+id/nav_host_fragment"
    android:layout_width="match_parent"
    android:layout_height="0dp"
    android:layout_weight="1"
    android:name="androidx.navigation.fragment.NavHostFragment"
    app:navGraph="@navigation/navigation"
    app:defaultNavHost="true" />
<com.google.android.material.bottomnavigation.BottomNavigationView
    android:id="@+id/bottom_nav_view"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom"
    app:menu="@menu/bottom_nav"
    style="@style/Widget.MaterialComponents.BottomNavigationView"
    >


</com.google.android.material.bottomnavigation.BottomNavigationView>

底部导航.xml:

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:id="@+id/action_next" android:title="@string/tornment_mode" android:icon="@drawable/ic_navigate_next_black_24dp" />

</menu>

最佳答案

无法“取消”导航事件 - 您需要将业务逻辑放在按钮/监听器本身上,并且仅在您真正想要导航时调用 navigate()

Creating event callbacks to the activity documentation通过使用 onAttach(Context) 获取对 Activity 的引用并设置属性、回调等,复习了一种将 Activity 和 Fragment 连接在一起的方法。

例如,您可能会考虑执行以下操作:

// In your Activity
var onNextClicked: () -> Unit = {}

// In your Activity's onCreate()
button.setOnClickListener = View.OnClickListener {
  onNextClicked.invoke()
}

// In your Fragment
override fun onAttach(context: Context) {
  (context as YourActivity).onNextClicked = {
    val teams = teamAdapter.getItems()
    if (validateSubmit(teams)){
      presenter.saveTeams(teams)
      findNavController().navigate(R.id.action_next)
    }
  }
}

每个 Fragment 都会设置它们希望下一个按钮执行的操作,从而让您完全控制按钮的操作。

如果您对 BottomNavigationView 死心塌地,而不是仅仅使用实际的 Button,您可能想要设置自己的 OnNavigationItemSelectedListener 并让该监听器调用 onNextClicked lambda,如果你正在使用这个例子。

关于android - 如何从 fragment 中拦截 MainActivity 的 BottomNavigationView 菜单点击,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53403692/

相关文章:

java - 使用光标适配器选中复选框时,使用删除按钮删除 ListView 中的项目

java - 尝试计算 Android 中两个坐标之间的距离时得到荒谬的值

string - 是否有正则表达式可以切换字符串中的字符大小写?

android - 如何在android底部导航 View 中更改文本填充

android - 底部导航上方的 Bottom Sheet

java - R.java 错误 : Class file collision

android - 我的应用程序没有出现在市场上

java - 安卓 : Constraint Layout Set Constraint top programmatically?

java - 防止在 RealmList 中保存时创建 Realm 对象

android - 如何在 Kotlin 中设置默认的 BottomNavigationView 选项卡?