Search code examples
androidnavigation-drawerandroid-jetpackandroid-architecture-navigationandroid-jetpack-navigation

Navigation drawer android switching betveen fragments?


OK,am new with navigation charts and data binding, tried to follow android documentation on this subject but simply can't understand what is going on.This is simply empty activity with nav drawer,but how,for a love of god, i switch to another fragment when i click on some item in nav drawer?

    public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {

    private AppBarConfiguration mAppBarConfiguration;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        Toolbar toolbar = findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        FloatingActionButton fab = findViewById(R.id.fab);
        fab.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
                        .setAction("Action", null).show();
            }
        });
        DrawerLayout drawer = findViewById(R.id.drawer_layout);
        NavigationView navigationView = findViewById(R.id.nav_view);
        // Passing each menu ID as a set of Ids because each
        // menu should be considered as top level destinations.
        mAppBarConfiguration = new AppBarConfiguration.Builder(
                R.id.nav_home_fragment, R.id.nav_gallery_fragment, R.id.nav_slideshow_fragment)
                .setDrawerLayout(drawer)
                .build();
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        NavigationUI.setupActionBarWithNavController(this, navController, mAppBarConfiguration);
        NavigationUI.setupWithNavController(navigationView, navController);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

    @Override
    public boolean onSupportNavigateUp() {
        NavController navController = Navigation.findNavController(this, R.id.nav_host_fragment);
        return NavigationUI.navigateUp(navController, mAppBarConfiguration)
                || super.onSupportNavigateUp();
    }

    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        switch (item.getItemId()) {
            case R.id.pocetni_ekran: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_home_fragment);
                break;
            }
            case R.id.galerija: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_gallery_fragment);
                break;
            }
            case R.id.slajdsou: {
                Navigation.findNavController(this, R.id.nav_host_fragment).navigate(R.id.nav_slideshow_fragment);
                break;
            }
        }
        item.setChecked(true);
return true;
    }
}

and my mobile navigation xml

    <?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/mobile_navigation"
    app:startDestination="@+id/nav_home_fragment">

    <fragment
        android:id="@+id/nav_home_fragment"
        android:name="com.nswd.slajdmeni.ui.home.HomeFragment"
        android:label="@string/menu_home"
        tools:layout="@layout/fragment_home">

        <action
            android:id="@+id/action_HomeFragment_to_HomeSecondFragment"
            app:destination="@id/nav_gallery_fragment" />
        <action
            android:id="@+id/action_nav_home_fragment_to_nav_slideshow_fragment"
            app:destination="@id/nav_slideshow_fragment" />
    </fragment>



    <fragment
        android:id="@+id/nav_gallery_fragment"
        android:name="com.nswd.slajdmeni.ui.gallery.GalleryFragment"
        android:label="@string/menu_gallery"
        tools:layout="@layout/fragment_gallery" />

    <fragment
        android:id="@+id/nav_slideshow_fragment"
        android:name="com.nswd.slajdmeni.ui.slideshow.SlideshowFragment"
        android:label="@string/menu_slideshow"
        tools:layout="@layout/fragment_slideshow" />
</navigation>

and my app bar xml

 <?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    tools:showIn="navigation_view">

    <group android:checkableBehavior="single">
        <item
            android:id="@+id/pocetni_ekran"
            android:icon="@drawable/ic_menu_camera"
            android:title="@string/menu_home" />
        <item
            android:id="@+id/galerija"
            android:icon="@drawable/ic_menu_gallery"
            android:title="@string/menu_gallery" />
        <item
            android:id="@+id/slajdsou"
            android:icon="@drawable/ic_menu_slideshow"
            android:title="@string/menu_slideshow" />
    </group>
</menu>

EDITED: posting my main_activity layout:

<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">

<com.google.android.material.navigation.NavigationView
    android:id="@+id/nav_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:fitsSystemWindows="true"
    app:headerLayout="@layout/nav_header_main"
    app:menu="@menu/activity_main_drawer" />

<include
    layout="@layout/app_bar_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

</androidx.drawerlayout.widget.DrawerLayout>


Solution

  • In your layout change the position to:

    <androidx.drawerlayout.widget.DrawerLayout>
    
      <include
        .../>
    
      <com.google.android.material.navigation.NavigationView
        android:layout_gravity="start"
        ../>
    
    </androidx.drawerlayout.widget.DrawerLayout>
    

    Then replace the IDs in the activity_main_drawer to match the id defined in the navigation.xml graph