gpt4 book ai didi

android - 底部导航 : NavigationUI. setupActionBarWithNavController() 不适用于 fragment 交换

转载 作者:行者123 更新时间:2023-12-04 23:52:37 25 4
gpt4 key购买 nike

背景: (如果你不想知道我是怎么得到问题的,可以忽略。只是一些关于 Android Studio 模板 Activity 的问题)我一直在研究底部导航,并尝试从 Android Studio 默认创建的底部导航 Activity 模板中复制 MainActivity.java 的代码。

Choosing template

我还从 Android Studio 找到了这个 UI 的文档。 Bottom Navigation

基本上,MainActivity中调用底部导航交互的主要代码应该是这样的:

    AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder(
R.id.navigation_home, R.id.navigation_dashboard, R.id.navigation_notifications)
.build();
NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(this, navController, appBarConfiguration);
NavigationUI.setupWithNavController(navView, navController);

我的问题:我的底部导航 xml 完美运行。但我刚刚找到 NavigationUI.setupActionBarWithNavController()NavigationUI.setupWithNavController()不工作。我的应用只是 闪停每当我运行它。有点奇怪,但我相信这两种方法是底部导航的官方用法。

主页.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:fitsSystemWindows="true"
android:layout_height="match_parent">

<com.google.android.material.appbar.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="@style/AppTheme.AppBarOverlay"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent">

<androidx.appcompat.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#267A9E"
app:popupTheme="@style/AppTheme.PopupOverlay" />

</com.google.android.material.appbar.AppBarLayout>


<fragment
android:id="@+id/frag_container"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="612dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toBottomOf="@id/bottom_navi"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="@navigation/nav_graph" />

<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navi"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="@menu/bottom_navigation" />

<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="@+id/fab"
android:layout_width="53dp"
android:layout_height="56dp"
android:layout_gravity="bottom|end"
android:layout_margin="@dimen/fab_margin"
app:backgroundTint="#4a90c9"
app:layout_constraintBottom_toTopOf="@+id/bottom_navi"
app:layout_constraintEnd_toEndOf="parent"
app:srcCompat="@drawable/plus" />


</androidx.constraintlayout.widget.ConstraintLayout>



解决方案:现在我使用传统的方式(设置点击监听器)来正确实现这个导航。这是老式的但非常灵活,因为我有我的个性化工具栏和侧边菜单。
  bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
navController= Navigation.findNavController(HomeActivity.this, R.id.frag_container);
switch (item.getItemId()) {
case R.id.bottom_navi_intake:
navController.navigate(R.id.action_WeightFragment_to_IntakeFragment);
break;
case R.id.bottom_navi_weight:
navController.navigate(R.id.action_IntakeFragment_to_WeightFragment);
break;
}
return true;
}
});
R.id.action_...是@navigation/nav.graph.xml 中的操作 ID:
<?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/nav_graph"
app:startDestination="@id/bottom_navi_intake">

<fragment
android:id="@+id/bottom_navi_intake"
android:name="com.example.dietdiary.IntakeFragment"
android:label="@string/first_fragment_label"
tools:layout="@layout/fragment_intake">

<action
android:id="@+id/action_IntakeFragment_to_WeightFragment"
app:destination="@id/bottom_navi_weight" />
</fragment>
<fragment
android:id="@+id/bottom_navi_weight"
android:name="com.example.dietdiary.WeightFragment"
android:label="@string/second_fragment_label"
tools:layout="@layout/fragment_weight">

<action
android:id="@+id/action_WeightFragment_to_IntakeFragment"
app:destination="@id/bottom_navi_intake" />
</fragment>

我的最终截图供您引用: final outcome

最佳答案

我遇到了同样的错误,我通过创建我的 解决了它自带工具栏然后将其设置为 支持操作栏如下。

最初,获取 工具栏 findviewbyid 或使用 数据绑定(bind) 并将其分配给一个变量。在这里我将它设置为变量工具栏,然后进一步使用它。

setSupportActionBar(toolbar)

然后我们必须将它添加到 应用栏配置 如下。
toolbar.setupWithNavController(navController, appBarConfiguration)

这个 错误 可能由于 而发生未建立的连接 之间工具栏导航 View .

希望这可以帮助。

关于android - 底部导航 : NavigationUI. setupActionBarWithNavController() 不适用于 fragment 交换,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61927892/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com