/* * MIT License * * Copyright (c) 2017 Jan Heinrich Reimer * * Permission is hereby granted, free of charge, to any person obtaining a copy * of this software and associated documentation files (the "Software"), to deal * in the Software without restriction, including without limitation the rights * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell * copies of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be included in all * copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ package com.heinrichreimersoftware.materialdrawer; import android.annotation.SuppressLint; import android.content.res.Configuration; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.support.v7.app.ActionBarDrawerToggle; import android.support.v7.app.AppCompatActivity; import android.support.v7.widget.Toolbar; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import com.heinrichreimersoftware.materialdrawer.structure.DrawerFragmentItem; import com.heinrichreimersoftware.materialdrawer.structure.DrawerItem; import com.heinrichreimersoftware.materialdrawer.structure.DrawerProfile; import com.heinrichreimersoftware.materialdrawer.theme.DrawerTheme; import java.util.List; @SuppressWarnings("unused") @SuppressLint("Registered") public class DrawerActivity extends AppCompatActivity { private DrawerFrameLayout mDrawer; private Toolbar mDefaultToolbar; private FrameLayout mFrame; private ActionBarDrawerToggle mDrawerToggle; private DrawerItem.OnItemClickListener mOnItemClickListener; private DrawerItem.OnItemClickListener mOnFixedItemClickListener; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); super.setContentView(R.layout.md_drawer_activity); mDrawer = (DrawerFrameLayout) findViewById(R.id.mdDrawerLayout); mDefaultToolbar = (Toolbar) findViewById(R.id.mdToolbar); mFrame = (FrameLayout) findViewById(R.id.mdFrame); setSupportActionBar(mDefaultToolbar); mDrawer.closeDrawer(); } /** * Set a {@link Toolbar} to act as the {@link android.support.v7.app.ActionBar}ActionBar for this Activity window. * You must take care of hiding the old {@link Toolbar} if necessary. * * @param toolbar Toolbar to set as the Activity's action bar */ @Override public void setSupportActionBar(@Nullable Toolbar toolbar) { if (toolbar != null) { if (toolbar != mDefaultToolbar && mDefaultToolbar.getVisibility() != View.GONE) { mDefaultToolbar.setVisibility(View.GONE); } super.setSupportActionBar(toolbar); mDrawerToggle = new ActionBarDrawerToggle(this, mDrawer, toolbar, R.string.md_content_description_drawer_open, R.string.md_content_description_drawer_close) { public void onDrawerClosed(View view) { invalidateOptionsMenu(); } public void onDrawerOpened(View drawerView) { invalidateOptionsMenu(); } }; mDrawerToggle.syncState(); mDrawer.addDrawerListener(mDrawerToggle); mDrawer.setOnItemClickListener(new DrawerItem.OnItemClickListener() { @Override public void onClick(DrawerItem item, long id, int position) { if (item instanceof DrawerFragmentItem && ((DrawerFragmentItem) item).hasFragment()) { switchFragment(((DrawerFragmentItem) item).getFragment()); } if (item.hasOnItemClickListener()) { item.getOnItemClickListener().onClick(item, id, position); } else { if (hasOnItemClickListener()) { mOnItemClickListener.onClick(item, id, position); } } } }); mDrawer.setOnFixedItemClickListener(new DrawerItem.OnItemClickListener() { @Override public void onClick(DrawerItem item, long id, int position) { if (item instanceof DrawerFragmentItem && ((DrawerFragmentItem) item).hasFragment()) { switchFragment(((DrawerFragmentItem) item).getFragment()); } if (item.hasOnItemClickListener()) { item.getOnItemClickListener().onClick(item, id, position); } else { if (hasOnItemClickListener()) { mOnFixedItemClickListener.onClick(item, id, position); } } } }); } } private void switchFragment(Fragment fragment){ FragmentManager fragmentManager = getSupportFragmentManager(); if(fragmentManager.findFragmentById(R.id.mdFrame) == fragment) return; FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction(); fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_NONE); if (fragmentManager.findFragmentById(R.id.mdFrame) != null) { fragmentTransaction.replace(R.id.mdFrame, fragment); } else { ((ViewGroup)findViewById(R.id.mdFrame)).removeAllViews(); fragmentTransaction.add(R.id.mdFrame, fragment); } fragmentTransaction.commit(); } @Override public boolean onOptionsItemSelected(MenuItem item) { return mDrawerToggle.onOptionsItemSelected(item) || super.onOptionsItemSelected(item); } @Override public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); mDrawerToggle.onConfigurationChanged(newConfig); } @Override protected void onPostCreate(Bundle savedInstanceState) { super.onPostCreate(savedInstanceState); mDrawerToggle.syncState(); } @Override public void setContentView(int layoutResID) { mFrame.removeAllViews(); View.inflate(this, layoutResID, mFrame); } @Override public void setContentView(View view) { mFrame.removeAllViews(); mFrame.addView(view); } @Override public void setContentView(View view, ViewGroup.LayoutParams params) { mFrame.removeAllViews(); mFrame.addView(view, params); } @Override public void addContentView(View view, ViewGroup.LayoutParams params) { mFrame.addView(view, params); } /** * Gets whether the enhanced drawer indicator is enabled * * @return true if the enhanced drawer indicator is enabled, false otherwise */ public boolean isDrawerIndicatorEnabled() { return mDrawerToggle != null && mDrawerToggle.isDrawerIndicatorEnabled(); } /** * Enable or disable the drawer indicator. The indicator defaults to enabled. * When the indicator is disabled, the ActionBar will revert to displaying the home-as-up indicator provided by the {@link android.app.Activity}'s theme in the android.R.attr.homeAsUpIndicator attribute instead of the animated drawer glyph. * * @param enabled true to enable, false to disable */ public DrawerActivity setDrawerIndicatorEnabled(boolean enabled) { if (mDrawerToggle != null) { mDrawerToggle.setDrawerIndicatorEnabled(enabled); } return this; } /** * Set the up indicator to display when the drawer indicator is not * enabled. *
* If you pass null
to this method, the default drawable from
* the theme will be used.
*
* @param indicator A drawable to use for the up indicator, or null to use
* the theme's default
* @see #setDrawerIndicatorEnabled(boolean)
*/
public void setHomeAsUpIndicator(Drawable indicator) {
if (mDrawerToggle != null) {
mDrawerToggle.setHomeAsUpIndicator(indicator);
}
}
/**
* Set the up indicator to display when the drawer indicator is not
* enabled.
*
* If you pass 0 to this method, the default drawable from the theme will
* be used.
*
* @param resId Resource ID of a drawable to use for the up indicator, or 0
* to use the theme's default
* @see #setDrawerIndicatorEnabled(boolean)
*/
public void setHomeAsUpIndicator(int resId) {
if (mDrawerToggle != null) {
mDrawerToggle.setHomeAsUpIndicator(resId);
}
}
/**
* Returns the fallback listener for Navigation icon click events.
*
* @return The click listener which receives Navigation click events from Toolbar when
* drawer indicator is disabled.
* @see #setToolbarNavigationClickListener(android.view.View.OnClickListener)
* @see #setDrawerIndicatorEnabled(boolean)
* @see #isDrawerIndicatorEnabled()
*/
public View.OnClickListener getToolbarNavigationClickListener() {
if (mDrawerToggle != null) {
return mDrawerToggle.getToolbarNavigationClickListener();
}
return null;
}
/**
* When DrawerToggle is constructed with a Toolbar, it sets the click listener on
* the Navigation icon. If you want to listen for clicks on the Navigation icon when
* DrawerToggle is disabled ({@link #setDrawerIndicatorEnabled(boolean)}, you should call this
* method with your listener and DrawerToggle will forward click events to that listener
* when drawer indicator is disabled.
*
* @see #setDrawerIndicatorEnabled(boolean)
*/
public void setToolbarNavigationClickListener(View.OnClickListener listener) {
if (mDrawerToggle != null) {
mDrawerToggle.setToolbarNavigationClickListener(listener);
}
}
/**
* Gets whether debug logging is enabled
*/
public boolean getLoggingEnabled() {
return mDrawer.getLoggingEnabled();
}
/**
* Sets whether debug logging is enabled
*
* @param loggingEnabled whether or not to enable debug logging
*/
public DrawerActivity setLoggingEnabled(boolean loggingEnabled) {
mDrawer.setLoggingEnabled(loggingEnabled);
return this;
}
/**
* Resets the drawer theme
*/
public DrawerActivity resetDrawerTheme() {
mDrawer.resetDrawerTheme();
return this;
}
/**
* Gets the drawer theme
*/
public DrawerTheme getDrawerTheme() {
return mDrawer.getDrawerTheme();
}
/**
* Sets the drawer theme
*
* @param theme Theme to set
*/
public DrawerActivity setDrawerTheme(DrawerTheme theme) {
mDrawer.setDrawerTheme(theme);
return this;
}
/**
* Sets the max drawer width from resources
*
* @param drawerMaxWidthResource Max drawer width resource to set
*/
public DrawerActivity setDrawerMaxWidthResource(int drawerMaxWidthResource) {
mDrawer.setDrawerMaxWidthResource(drawerMaxWidthResource);
return this;
}
/**
* Resets the max drawer width
*/
public DrawerActivity resetDrawerMaxWidth() {
mDrawer.resetDrawerMaxWidth();
return this;
}
/**
* Gets the max drawer width
*/
public int getDrawerMaxWidth() {
return mDrawer.getDrawerMaxWidth();
}
/**
* Sets the max drawer width
*
* @param drawerMaxWidth Max drawer width to set
*/
public DrawerActivity setDrawerMaxWidth(int drawerMaxWidth) {
mDrawer.setDrawerMaxWidth(drawerMaxWidth);
return this;
}
/**
* Adds a profile to the drawer view
*
* @param profile Profile to add
*/
public DrawerActivity addProfile(DrawerProfile profile) {
mDrawer.addProfile(profile);
return this;
}
/**
* Gets all profiles from the drawer view
*
* @return Profiles from the drawer view
*/
public List