【问题标题】:How I switch fragment from another fragment on bottom navigtion view?如何在底部导航视图上从另一个片段切换片段?
【发布时间】:2020-09-01 12:47:19
【问题描述】:

我使用底部导航视图 [1.HomeFragment 2.DashboardFragment 3.ProfileFragment]。我想从 HomeFragment 导航到 DashboardFragment。

这是我使用的代码,但它不起作用,它关闭了应用程序

b3.setOnClickListener(new OnClickListener(){
@override
    public void onClick(View view) {
    Fragment fragment = new DashboardFragment();
    FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
    FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
    fragmentTransaction.replace(R.id.navigation_dashboard, fragment);
    fragmentTransaction.addToBackStack(null);
    fragmentTransaction.commit();
    }
});

而且我用这个方法也行不通:

getSupportFragmentManager().beginTransaction().replace(R.id.navigation_dashboard, new DashboardFragment()).commit();

【问题讨论】:

    标签: android android-fragments fragment


    【解决方案1】:

    如果您要使用 Fragments,我强烈建议您使用导航组件。

    它更易于使用,使您的代码更简洁,并在 Google PlayStore 上的许多前 1000 名应用中使用。 如果您想阅读更多内容Android Developers Website

    【讨论】:

    • 感谢您分享有关导航视图的详细信息。
    【解决方案2】:

    如果您使用的是 BottomNavigationView,那么您必须像这样实现 setOnNavigationItemSelectedListener:

      bottomNavigationView.setOnNavigationItemSelectedListener(
        new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                switch (item.getItemId()) {
                    case R.id.action_favorites:
    
                        break;
                    case R.id.action_schedules:
    
                        break;
                    case R.id.action_music:
    
                        break;
                }
                return false;
            }
    

    });

    【讨论】:

    • 感谢@MuhammadSajjadSaleem,但我想通过按钮(OnClickListener)切换导航,我已经实现了总底部导航视图结构。如果您知道使用按钮单击的导航方法,请分享给我。
    【解决方案3】:

    我强烈推荐使用导航组件。 https://developer.android.com/guide/navigation/navigation-getting-started

      // Java language implementation
      implementation "androidx.navigation:navigation-fragment:$nav_version"
      implementation "androidx.navigation:navigation-ui:$nav_version"
    
      // Kotlin
      implementation "androidx.navigation:navigation-fragment-ktx:$nav_version"
      implementation "androidx.navigation:navigation-ui-ktx:$nav_version"
    
      // Feature module Support
      implementation "androidx.navigation:navigation-dynamic-features-fragment:$nav_version"
    

    创建一个菜单:res/menu/my_navigation_items.xml:

    <menu xmlns:android="http://schemas.android.com/apk/res/android">
         <item android:id="@+id/action_home"
              android:title="@string/menu_home"
              android:icon="@drawable/ic_home" />
         <item android:id="@+id/action_dashboard"
              android:title="@string/menu_dashboard"
              android:icon="@drawable/ic_dashboard" />
         <item android:id="@+id/action_profile"
              android:title="@string/menu_profile"
              android:icon="@drawable/ic_profile" />
     </menu>
    

    并使用 app:menu="@menu/my_navigation_items" 将其分配为 bottomNavigationView 菜单:

    <com.google.android.material.bottomnavigation.BottomNavigationView
         xmlns:android="http://schemas.android.com/apk/res/android"
         xmlns:app="http://schema.android.com/apk/res/res-auto"
         android:id="@+id/navigation"
         android:layout_width="match_parent"
         android:layout_height="56dp"
         android:layout_gravity="start"
         app:menu="@menu/my_navigation_items" />
    

    最后最重要的是,在 androidx.fragment.app.FragmentContainerView 中使用导航图:

        <?xml version="1.0" encoding="utf-8"?>
    <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.fragment.app.FragmentContainerView
            android:id="@+id/nav_host_container"
            android:name="androidx.navigation.fragment.NavHostFragment"
            android:layout_width="match_parent"
            android:layout_height="0dp"
            android:layout_weight="1"
            app:navGraph="@navigation/navigation_graph" />
    
        <com.google.android.material.bottomnavigation.BottomNavigationView
            android:id="@+id/navigation"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:menu="@menu/my_navigation_items"
            app:labelVisibilityMode="labeled"/>
    
    </LinearLayout>
    

    此图表将自动实例化您的片段。你应该在 res/navigation/ 中创建它:

    <?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"
        app:startDestination="@id/home_fragment">
        <fragment
            android:id="@+id/home_fragment"
            android:name="<yourpackage>.HomeFragment"
            android:label="@string/home"
            tools:layout="@layout/fragment_home" />
        <fragment
            android:id="@+id/dashboard_fragment"
            android:name="<yourpackage>.DashboardFragment"
            android:label="@string/informations"
            tools:layout="@layout/fragment_report" />
        <fragment
            android:id="@+id/profile_fragment"
            android:name="<yourpackage>.ProfileFragment"
            android:label="@string/profile"
            tools:layout="@layout/profile_layout" />
    </navigation>
    

    【讨论】:

      【解决方案4】:

      试试这个代码:

      活动中:

       b3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      getSupportFragmentManager().beginTransaction().replace(R.id.navigation_dashboard, new DashboardFragment(),"Dashboard").commit();
                  }
              });
      

      在片段中:

      b3.setOnClickListener(new View.OnClickListener() {
                  @Override
                  public void onClick(View v) {
                      getFragmentManager().beginTransaction().replace(R.id.navigation_dashboard, new DashboardFragment(),"Dashboard").commit();
                  }
              });
      

      【讨论】:

      • 这个方法我已经用过了,不行,我用这个方法时当前fragment只出现无法切换另一个fragment。
      • 编辑我的答案。
      猜你喜欢
      • 1970-01-01
      • 2017-03-20
      • 2020-10-04
      • 2021-12-03
      • 1970-01-01
      • 2013-12-19
      • 1970-01-01
      • 2022-01-06
      • 2022-11-03
      相关资源
      最近更新 更多