我正在尝试将BottomNavigationView与Navigation Editor一起使用。我已经实现了给定的一切,但是只显示了主页,当我更改选项卡时,它不会更改片段。
这是主要的活动代码:
public class MainActivity extends AppCompatActivity {
BottomNavigationView mainBottomNavigation;
NavController navController;
NavHostFragment navHostFragment;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBottomNavigation = findViewById(R.id.bottom_navigation);
navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
NavigationUI.setupWithNavController(mainBottomNavigation, Navigation.findNavController(this,R.id.nav_host_fragment));
} }
这是主要的活动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:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="@+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toTopOf="@+id/bottom_navigation"
app:defaultNavHost="true"
app:navGraph="@navigation/nav_graph"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="@+id/bottom_navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="@menu/navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
这是菜单文件:
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/navigation_location"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_location" />
<item
android:id="@+id/navigation_sightseeing"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_sightseeing" />
<item
android:id="@+id/navigation_home"
android:icon="@drawable/ic_home_black_24dp"
android:title="@string/title_home" />
<item
android:id="@+id/navigation_gallery"
android:icon="@drawable/ic_dashboard_black_24dp"
android:title="@string/title_gallery" />
<item
android:id="@+id/navigation_contact_us"
android:icon="@drawable/ic_notifications_black_24dp"
android:title="@string/title_contact_us" />
</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/nav_graph"
app:startDestination="@id/navigation_home">
<fragment
android:id="@+id/navigation_location"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_location"
tools:layout="@layout/fragment_location" />
<fragment
android:id="@+id/navigation_sightseeing"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_sightseeing"
tools:layout="@layout/fragment_sight_seeing" />
<fragment
android:id="@+id/navigation_home"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_home"
tools:layout="@layout/fragment_home" />
<fragment
android:id="@+id/navigation_gallery"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_gallery"
tools:layout="@layout/fragment_gallery" />
<fragment
android:id="@+id/navigation_contact_us"
android:name="com.wedding.rashmilind.HomeFragment"
android:label="fragment_contact_us"
tools:layout="@layout/fragment_contact_us" />
</navigation>
答案 0 :(得分:0)
要进行导航,您需要调用NavController的 navigate()
方法。
使用NavController
(在NavHost
中管理应用程序导航的对象)完成到目的地的导航。每个NavHost都有自己的对应NavController。
要获取片段,活动或视图的NavController,请使用以下方法之一:
NavHostFragment.findNavController(Fragment)
// -- or --
Navigation.findNavController(Activity, @IdRes int viewId)
// -- or --
Navigation.findNavController(View)
一旦检索到NavController,请使用其navigate()
方法导航到目标位置。 navigation()方法接受操作或目标的资源ID。
然后,在导航按钮的onClick
上设置以下内容:
Navigation.findNavController(view).navigate(R.id.viewTransactionsAction);