如何在底部导航视图上从另一个片段切换片段?

时间:2020-09-01 12:47:19

标签: android android-fragments fragment

我使用底部导航视图[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();

4 个答案:

答案 0 :(得分:0)

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

它更易于使用,使您的代码更整洁,并且在Google PlayStore上的前1000个应用程序中得到了广泛使用。 如果您想阅读更多Android Developers Website

答案 1 :(得分:0)

尝试以下代码:

活动中:

//function 
function whois($site)
    {
    
     $domain = $site;
    
     $servers = array(
      ".com" => "whois.internic.net",
    
     );
    
     if (!isset($servers[$ext])) {
       return false;
     }
    
     $nic_server = $servers[$ext];
    
     $output = '';
    
     // connect to whois server:
     if ($conn = fsockopen($nic_server, 43)) {
      fwrite($conn, $domain."\r\n");
      while (!feof($conn)) {
       $output .= fgets($conn, 128);
      }
      fclose($conn);
     } else {
       return false;
     }
     return $output;
    }
    $site ="example.com";
    $whoisinfo = whois($site);

    //updating data
    $conn->query("UPDATE tablename SET whois='$whoisinfo' WHERE site='$site'");

在片段中:

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

答案 2 :(得分:0)

如果您使用的是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;
        }

});

答案 3 :(得分:0)

我强烈建议您使用导航组件。 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>