1

I've been working on a navigation drawer for my application and I've decided to build it with an ExpandableListView.

My question is how can I add a header (for example a user image profile and user's fullname) to the top of the navigation drawer?

I'm using a custom ExpandableListAdapter.

If any code is needed, just ask. I'll post it right away.

EDIT

This is my implementation of the navigation drawer:

ExpandableListAdapter.java

package co.eshg.drawertest;

import java.util.HashMap;
import java.util.List;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class ExpandableListAdapter extends BaseExpandableListAdapter {

    private Context context;
    private List<String> listDataItem; // header titles
    // child data in format of header title, child title
    private HashMap<String, List<String>> listDataChild;

    public ExpandableListAdapter(Context context, List<String> listDataItem,
                                 HashMap<String, List<String>> listChildData) {
        this.context = context;
        this.listDataItem = listDataItem;
        this.listDataChild = listChildData;
    }

    @Override
    public Object getChild(int groupPosition, int childPosititon) {
        return this.listDataChild.get(this.listDataItem.get(groupPosition)).get(childPosititon);
    }

    @Override
    public long getChildId(int groupPosition, int childPosition) {
        return childPosition;
    }

    @Override
    public View getChildView(int groupPosition, final int childPosition,
                             boolean isLastChild, View convertView, ViewGroup parent) {

        final String childText = (String) getChild(groupPosition, childPosition);

        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.drawer_child_item, null);
        }

        TextView txtListChild = (TextView) convertView
                .findViewById(R.id.tvChildItem);

        txtListChild.setText(childText);
        return convertView;
    }

    @Override
    public int getChildrenCount(int groupPosition) {
        List childList = listDataChild.get(listDataItem.get(groupPosition));
        if (childList != null && ! childList.isEmpty()) {
            return childList.size();
        }
        return 0;
    }

    @Override
    public Object getGroup(int groupPosition) {
        return this.listDataItem.get(groupPosition);
    }

    @Override
    public int getGroupCount() {
        return this.listDataItem.size();
    }

    @Override
    public long getGroupId(int groupPosition) {
        return groupPosition;
    }

    @Override
    public View getGroupView(int groupPosition, boolean isExpanded,
                             View convertView, ViewGroup parent) {
        String headerTitle = (String) getGroup(groupPosition);
        if (convertView == null) {
            LayoutInflater inflater = (LayoutInflater) this.context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            convertView = inflater.inflate(R.layout.drawer_list_item, null);
        }

        ImageView indicator = (ImageView)convertView.findViewById(R.id.imgIndicator);
        if ( getChildrenCount( groupPosition ) == 0 ) {
            indicator.setVisibility( View.INVISIBLE );
        } else {
            indicator.setVisibility( View.VISIBLE );
            indicator.setImageResource( isExpanded ? R.drawable.ic_keyboard_arrow_down_white_24dp
                    : R.drawable.ic_keyboard_arrow_up_white_24dp );
        }

        TextView lblListHeader = (TextView) convertView
                .findViewById(R.id.tvListItem);
        lblListHeader.setText(headerTitle);

        return convertView;
    }

    @Override
    public boolean hasStableIds() {
        return false;
    }

    @Override
    public boolean isChildSelectable(int groupPosition, int childPosition) {
        return true;
    }
}

activity_drawer_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <FrameLayout
        android:id="@+id/activity_frame"
        android:layout_height="match_parent"
        android:layout_width="match_parent"/>


    <ExpandableListView
        android:id="@+id/left_drawer"
        android:layout_height="match_parent"
        android:layout_width="240dp"
        android:layout_gravity="start"
        android:choiceMode="singleChoice"
        android:groupIndicator="@android:color/transparent"
        android:divider="@android:color/transparent"
        android:background="#444444" />

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

drawer_header.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="100dp"
    android:orientation="vertical"
    android:weightSum="1">
        <TextView
            android:id="@+id/name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginLeft="16dp"
            android:textColor="#ffffff"
            android:text="Example Example"
            android:textSize="14sp"
            android:textStyle="bold"
            android:layout_centerVertical="true"
            android:layout_alignStart="@+id/email" />

        <TextView
            android:id="@+id/email"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textColor="#ffffff"
            android:layout_marginLeft="16dp"
            android:text="example@gmail.com"
            android:textSize="14sp"
            android:textStyle="normal"
            android:layout_alignBottom="@+id/circleView"
            android:layout_toEndOf="@+id/circleView" />

    <de.hdodenhof.circleimageview.CircleImageView
        android:layout_width="60dip"
        android:layout_height="60dip"
        android:src="@drawable/ic_face_white_48dp"
        android:layout_marginLeft="20dp"
        android:layout_marginTop="20dp"
        android:id="@+id/circleView"
        android:layout_alignParentTop="true"
        android:layout_alignParentStart="true" />
</RelativeLayout>
UltraAlkaline
  • 61
  • 1
  • 8

1 Answers1

0

Edit 2:

To add a header to your existing navigation drawer layout, you can simply add a header xml value and reference another xml layout.

The above will not work with your implementation.

With your custom NavigationView with the FrameLayout and ExpandableListView, try this.

YourActivity.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
    android:id="@+id/dl_drawer_layout"
    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">

        <FrameLayout hold the fragments />

    <include
        layout="@layout/your_navigation_view.xml"/>

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

your_navigation_view.cml

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.NavigationView
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/nv_navigation_view"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_gravity="start">

    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:orientation="vertical">

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

        <ExpandableListView
            android:id="@+id/left_drawer"
            android:layout_height="match_parent"
            android:layout_width="240dp"
            android:layout_gravity="start"
            android:choiceMode="singleChoice"
            android:groupIndicator="@android:color/transparent"
            android:divider="@android:color/transparent"
            android:background="#444444" />
    </LinearLayout>
</android.support.design.widget.NavigationView>

Edit

Use this as a reference for custom NavigationDrawer Reference

You will have to make all the custom layouts and manage them manually.

Community
  • 1
  • 1
AnxGotta
  • 1,006
  • 7
  • 28
  • I tried your solution, but it didn't really fix anything. It hides my whole ExpandableListView and replaces it with the NavigationView. I tried setting the NavigationView's height to a fixed height, but it didn't work. – UltraAlkaline Oct 19 '15 at 14:36
  • It's pretty difficult to give you advice because I don't know your current implementation. You need to put your ExpandableListView inside the NavigationView with the solutions I presented. I didn't realize you weren't using the NavigationView implementation, which is pretty standard for compatibility reasons. – AnxGotta Oct 19 '15 at 14:57
  • Check Edit 2 and let me know how that looks. – AnxGotta Oct 19 '15 at 15:33
  • This is how I want it to look like: http://imgur.com/xlsXvd7 This is how it looks like: http://imgur.com/a/JMZMN – UltraAlkaline Oct 19 '15 at 16:32
  • See how this works for you. New code above.Adjust the activity code I posted above to fit your needs... toolbar, etc – AnxGotta Oct 19 '15 at 16:48