Search code examples
javanavigationview

Reload navigationView content by pressing an id in the navigationView


I'm currently working on an app in which I would like to change the lower part of a navigationView by pressing a pic in the header of the same navigationView.

My navigationView is build with a header layout and an another layout for the lower part. The header contains some pics. Each pics is supposed to lead to a different layout for the below part.

I have prepared few different menu, that are corresponding to different category. Each menu is integrated in different layout with unique id.

For the moment, the home category is showed when the navigationView is opened and I do not understand how I am supposed to reload the lower part by pressing a pic.

This is the part of the code in the mainActivity, in the onCreate

        //ACTIONBAR
    Toolbar mToolbar = findViewById(R.id.toolbar);
    setSupportActionBar(mToolbar);
    assert getSupportActionBar() != null;
    getSupportActionBar().setTitle("");

    //NAVIGATION VIEW
    DrawerLayout drawer = findViewById(R.id.drawer_layout);
    NavigationView navigationView = findViewById(R.id.nav_view_home);
    ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
            this, drawer, mToolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
    drawer.addDrawerListener(toggle);
    toggle.syncState();
    navigationView.setNavigationItemSelectedListener(this);

See, there the nav_view_home id is loaded and by pressing a pic in this navigationView header, I would like to see the lower part changing. For now, I have prepared different navigationView that are made with the same header : - nav_view_movie - nav_view_music - ...

Code of the drawer layout for home

<android.support.v4.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">

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

    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view_home"
        android:layout_width="250dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:theme="@style/NavigationTheme"
        android:fitsSystemWindows="true"
        android:background="#6b6b6b"
        app:headerLayout="@layout/format_soundsorganisationdrawerheader"
        app:menu="@menu/categorie_menu"
        app:itemTextColor="@drawable/nav_view_item_textcolor"
        app:itemBackground="@drawable/nav_view_item_background" >

    <include layout="@layout/format_soundsorganisationdrawerheader" />

    </android.support.design.widget.NavigationView>

</android.support.v4.widget.DrawerLayout>

Code of the drawer layout for movie

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

<android.support.design.widget.NavigationView
    android:id="@+id/nav_view_home"
    android:layout_width="250dp"
    android:layout_height="match_parent"
    android:layout_gravity="start"
    android:theme="@style/NavigationTheme"
    android:fitsSystemWindows="true"
    android:background="#6b6b6b"
    app:headerLayout="@layout/format_soundsorganisationdrawerheader"
    app:menu="@menu/categorie_menu"
    app:itemTextColor="@drawable/nav_view_item_textcolor"
    app:itemBackground="@drawable/nav_view_item_background" >

<include layout="@layout/format_soundsorganisationdrawerheader" />

</android.support.design.widget.NavigationView>

The header code for the navigationView layout

    <LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="#303030"
    android:gravity="center"
    android:orientation="horizontal"
    android:theme="@style/ThemeOverlay.AppCompat.Dark">

    <ImageView
        android:id="@+id/imageView_pic_movie"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/nav_header_desc"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        app:srcCompat="@drawable/pic_movie" />

    <ImageView
        android:id="@+id/imageView_pic_audio"
        android:layout_width="60dp"
        android:layout_height="60dp"
        android:contentDescription="@string/nav_header_desc"
        android:paddingEnd="10dp"
        android:paddingStart="10dp"
        app:srcCompat="@drawable/pic_audio" />
</LinearLayout>

So, as you see, the home layout is made of the header and the categorie_menu. For now I have tried to reload the complete layout when pressing the pic with imageView_pic_movie id by using another layout made of the same header and another menu named categorie_menu_movie but without success.

Maybe it's not possible, or maybe it's not the best way either ? There is maybe another way to use the same layout but just reload the menu instead of all the layout ?

In any case, I do not understand how am I supposed to do that.

Can you help me please ?


Solution

  • Ok, so my way to do seems to be a terrible idea. After some research, it seems that we can use visible true or visible false for an item or a group in a menu.

    So it's just necessary to do an onClick method and set the group visible or invisible when clicking on a pic or another.

    Source : Change NavigationView items when user is logged