W
W
wolfak2016-09-28 14:48:52
Android
wolfak, 2016-09-28 14:48:52

How to customize Head in Navigation Drawer Activity menu?

Good afternoon. I am creating a new application for android and using the standard application template - Navigation Drawer Activity and ran into a problem that I did not find a solution on the Internet.
I use fragment replacement in the standard way, when clicking on the item element of the menu item:

public boolean onNavigationItemSelected(MenuItem item) {

    // Создадим новый фрагмент
    Fragment fragment = null;
    Class fragmentClass = null;

    // Handle navigation view item clicks here.
    int id = item.getItemId();

    if (id == R.id.nav_camera) {
        // Handle the camera action
        fragmentClass = FirstFragment.class;

    } else if (id == R.id.nav_gallery) {
        fragmentClass = SecondFragment.class;
    } else if (id == R.id.nav_slideshow) {

    } else if (id == R.id.nav_manage) {

    } else if (id == R.id.nav_share) {

    } else if (id == R.id.nav_send) {

    }

    try {
        fragment = (Fragment) fragmentClass.newInstance();
    } catch (Exception e) {
        e.printStackTrace();
    }

    // Вставляем фрагмент, заменяя текущий фрагмент
    FragmentManager fragmentManager = getSupportFragmentManager();
    fragmentManager.beginTransaction().replace(R.id.container, fragment).commit();
    // Выделяем выбранный пункт меню в шторке
    item.setChecked(true);
    // Выводим выбранный пункт в заголовке
    setTitle(item.getTitle());

    DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
    drawer.closeDrawer(GravityCompat.START);
    return true;
}

As described here .
But how can you replace a fragment by clicking not on a menu item, but, for example, on an image from nav_header_main.xml ? I want to implement a transition to a user profile like a VK application, Avito or aliexpress (I clicked on the avatar at the top of the menu or on the login, you need to go to the profile), but I could not figure out how to replace the fragment by clicking on the markup from this file. Sample code from this file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout        
android:layout_width="match_parent"
android:id="@+id/headerView"
android:layout_height="@dimen/nav_header_height"
android:background="@drawable/side_nav_bar"
android:gravity="bottom"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:theme="@style/ThemeOverlay.AppCompat.Dark">

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:src="@android:drawable/sym_def_app_icon" />

<TextView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:paddingTop="@dimen/nav_header_vertical_spacing"
    android:text="Android Studio"
    android:textAppearance="@style/TextAppearance.AppCompat.Body1" />

<TextView
    android:id="@+id/textView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="[email protected]" />

</LinearLayout>

I hope for your help! Thank you very much in advance!

Answer the question

In order to leave comments, you need to log in

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question