de.schramke.android.navdrawer.NavigationDrawer.java Source code

Java tutorial

Introduction

Here is the source code for de.schramke.android.navdrawer.NavigationDrawer.java

Source

/* NavigationDrawer.java
 *
 * Copyright (c) 2014 Bjrn Schramke (bjoern.schramke@init.de)
 * Copyright (c) 2014 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package de.schramke.android.navdrawer;

import android.app.Activity;
import android.graphics.Color;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.Collection;
import java.util.HashSet;
import java.util.Set;

/**
 * Created by bschramke on 05.02.14.
 */
public class NavigationDrawer implements AdapterView.OnItemClickListener, AdapterView.OnItemLongClickListener,
        AdapterView.OnItemSelectedListener, DrawerLayout.DrawerListener {

    private Activity activity;
    private DrawerLayout drawerLayout;
    private ListView drawerList;
    private Menu drawerMenu;

    private ItemViewBuilder itemViewBuilder = new DefaultItemViewBuilder();

    private Set<NavigationDrawerListener> listeners;
    private Set<NavigationDrawerItemListener> itemListeners;
    private Set<DrawerLayout.DrawerListener> internListeners;

    public NavigationDrawer(Activity activity) throws IllegalArgumentException {
        this.activity = activity;
        this.drawerLayout = (DrawerLayout) activity.findViewById(R.id.drawer_layout);
        this.drawerList = (ListView) activity.findViewById(R.id.drawer_list);

        listeners = new HashSet<NavigationDrawerListener>();
        itemListeners = new HashSet<NavigationDrawerItemListener>();
        internListeners = new HashSet<DrawerLayout.DrawerListener>();

        if (activity instanceof NavigationDrawerListener) {
            listeners.add((NavigationDrawerListener) activity);
        }

        if (activity instanceof NavigationDrawerItemListener) {
            itemListeners.add((NavigationDrawerItemListener) activity);
        }

        if (this.drawerLayout == null) {
            final String errerMsg = activity.getString(R.string.exception_drawer_layout);
            throw new IllegalArgumentException(errerMsg);
        }

        if (this.drawerList == null) {
            final String errerMsg = activity.getString(R.string.exception_drawer_list);
            throw new IllegalArgumentException(errerMsg);
        }

        this.drawerLayout.setDrawerListener(this);
        this.drawerList.setOnItemClickListener(this);
        this.drawerList.setOnItemLongClickListener(this);
        this.drawerList.setOnItemSelectedListener(this);
        this.drawerLayout.closeDrawers();

        this.drawerMenu = new android.support.v7.internal.view.menu.MenuBuilder(activity);
        if (activity instanceof NavigationDrawerCreateListener) {
            ((NavigationDrawerCreateListener) activity).onCreateNavigationDrawerMenu(drawerMenu);
        }

        this.drawerList.setAdapter(new NavDrawerAdapter());
    }

    public DrawerLayout getDrawerLayout() {
        return drawerLayout;
    }

    public void setItemViewBuilder(ItemViewBuilder viewBuilder) {
        this.itemViewBuilder = viewBuilder;
    }

    /**
     * @see {Set.add(Object object)}
     */
    public boolean addItemListener(final NavigationDrawerItemListener listener) {
        return itemListeners.add(listener);
    }

    /**
     * @see {Set.addAll(Collection<?> collection)}
     */
    public boolean addItemListeners(final Collection<NavigationDrawerItemListener> listeners) {
        return itemListeners.addAll(listeners);
    }

    /**
     * @see {Set.remove(Object object)}
     */
    public boolean removeItemListener(final NavigationDrawerItemListener listener) {
        return itemListeners.remove(listener);
    }

    /**
     * @see {Set.removeAll(Collection<?> collection)}
     */
    public boolean removeItemListeners(final Collection<NavigationDrawerItemListener> listeners) {
        return itemListeners.removeAll(listeners);
    }

    public ActionBarDrawerToggle createDrawerToggle(int openDrawerContentDescRes, int closeDrawerContentDescRes) {
        ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this.activity, this.drawerLayout,
                openDrawerContentDescRes, closeDrawerContentDescRes) {
            public void onDrawerClosed(View view) {
                NavigationDrawer.this.activity.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }

            public void onDrawerOpened(View drawerView) {
                NavigationDrawer.this.activity.invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
            }
        };

        internListeners.add(drawerToggle);

        return drawerToggle;
    }

    //**************************************************************************
    //* implement interface AdapterView.OnItemClickListener
    //**************************************************************************
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

        for (NavigationDrawerItemListener listener : itemListeners) {
            listener.onDrawerItemClick(position, id);
        }

        drawerLayout.closeDrawers();
    }

    //**************************************************************************
    //* implement interface AdapterView.OnItemLongClickListener
    //**************************************************************************
    @Override
    public boolean onItemLongClick(AdapterView<?> parent, View view, int position, long id) {

        boolean returnValue = false;

        for (NavigationDrawerItemListener listener : itemListeners) {
            returnValue |= listener.onDrawerItemLongClick();
        }

        return returnValue;
    }

    //**************************************************************************
    //* implement interface AdapterView.OnItemSelectedListener
    //**************************************************************************
    @Override
    public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
        for (NavigationDrawerItemListener listener : itemListeners) {
            listener.onDrawerItemSelected();
        }
    }

    @Override
    public void onNothingSelected(AdapterView<?> parent) {

    }

    //**************************************************************************
    //* DrawerLayout delegate methods
    //**************************************************************************
    public void close() {
        this.drawerLayout.closeDrawers();
    }

    //**************************************************************************
    //* ListView delegate methods
    //**************************************************************************

    public ListAdapter getAdapter() {
        return drawerList.getAdapter();
    }

    public void setAdapter(BaseAdapter adapter) {
        drawerList.setAdapter(adapter);
    }

    public void addHeaderView(View v, Object data, boolean isSelectable) {
        drawerList.addHeaderView(v, data, isSelectable);
    }

    public void addHeaderView(View v) {
        drawerList.addHeaderView(v);
    }

    public int getHeaderViewsCount() {
        return drawerList.getHeaderViewsCount();
    }

    public boolean removeHeaderView(View v) {
        return drawerList.removeHeaderView(v);
    }

    public void addFooterView(View v, Object data, boolean isSelectable) {
        drawerList.addFooterView(v, data, isSelectable);
    }

    public void addFooterView(View v) {
        drawerList.addFooterView(v);
    }

    public int getFooterViewsCount() {
        return drawerList.getFooterViewsCount();
    }

    public boolean removeFooterView(View v) {
        return drawerList.removeFooterView(v);
    }

    //**************************************************************************
    //* implement interface DrawerLayout.DrawerListener
    //**************************************************************************
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        for (DrawerLayout.DrawerListener listener : internListeners) {
            listener.onDrawerSlide(drawerView, slideOffset);
        }

    }

    @Override
    public void onDrawerOpened(View drawerView) {
        for (DrawerLayout.DrawerListener listener : internListeners) {
            listener.onDrawerOpened(drawerView);
        }

        for (NavigationDrawerListener listener : listeners) {
            listener.onDrawerOpened(drawerView);
        }
    }

    @Override
    public void onDrawerClosed(View drawerView) {
        for (DrawerLayout.DrawerListener listener : internListeners) {
            listener.onDrawerClosed(drawerView);
        }

        for (NavigationDrawerListener listener : listeners) {
            listener.onDrawerClosed(drawerView);
        }
    }

    @Override
    public void onDrawerStateChanged(int newState) {
        for (DrawerLayout.DrawerListener listener : internListeners) {
            listener.onDrawerStateChanged(newState);
        }

        for (NavigationDrawerListener listener : listeners) {
            listener.onDrawerStateChanged(newState);
        }
    }

    public interface NavigationDrawerListener {
        public void onDrawerClosed(View drawerView);

        public void onDrawerOpened(View drawerView);

        public void onDrawerStateChanged(int newState);
    }

    public interface NavigationDrawerItemListener {
        public void onDrawerItemClick(int position, long id);

        public boolean onDrawerItemLongClick();

        public void onDrawerItemSelected();
    }

    public interface NavigationDrawerCreateListener {
        public void onCreateNavigationDrawerMenu(Menu menu);
    }

    public interface ItemViewBuilder {

        public View getView(MenuItem item, View convertView, ViewGroup parent);
    }

    private class NavDrawerAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return drawerMenu.size();
        }

        @Override
        public MenuItem getItem(int position) {
            return drawerMenu.getItem(position);
        }

        @Override
        public long getItemId(int position) {
            MenuItem item = getItem(position);
            return (item != null) ? item.getItemId() : position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            final MenuItem item = getItem(position);
            return itemViewBuilder.getView(item, convertView, parent);
        }
    }

    private class DefaultItemViewBuilder implements ItemViewBuilder {

        @Override
        public View getView(MenuItem item, View convertView, ViewGroup parent) {

            View itemView;
            if (convertView != null) {
                itemView = convertView;
            } else {
                LayoutInflater inflater = activity.getLayoutInflater();
                itemView = inflater.inflate(android.R.layout.simple_list_item_1, parent, false);
            }

            if (item.hasSubMenu()) {
                itemView.setBackgroundColor(Color.GREEN);
            }

            TextView text1 = (TextView) itemView.findViewById(android.R.id.text1);
            text1.setText(item.getTitle());

            return itemView;
        }
    }
}