Skip to content

Commit

Permalink
* add new listener which allows you to hook into the click behavior o…
Browse files Browse the repository at this point in the history
…f the `MiniDrawer` and prevent default behavior if needed

 * FIX mikepenz#1351
  • Loading branch information
mikepenz committed Jun 28, 2016
1 parent 85a495d commit 7e06953
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 9 deletions.
5 changes: 5 additions & 0 deletions MIGRATION.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
###Upgrade Notes

#### v5.3.1 -> v5.3.2
* the `withOnMiniDrawerItemClickListener` was renamed to `withOnMiniDrawerItemOnClickListener`
* added new separate `OnMiniDrawerItemClickListener` which allows to hook into the default behavior, and prevent it if necessary
* NOTE: this one now uses the `withOnMiniDrawerItemClickListener` method.

#### v5.2.0 -> v5.2.1
* the `SecondaryDrawerItem` is now a subclass of the `PrimaryDrawerItem` (extends `PrimaryDrawerItem`). If you have an `if` which checks for the type with `instanceOf` make sure you check for the `SecondaryDrawerItem` first. (`secondaryDrawerItem instanceOf PrimaryDrawerItem == true`)

Expand Down
61 changes: 52 additions & 9 deletions library/src/main/java/com/mikepenz/materialdrawer/MiniDrawer.java
Original file line number Diff line number Diff line change
Expand Up @@ -150,27 +150,42 @@ public MiniDrawer withEnableSelectedMiniDrawerItemBackground(boolean enableSelec
* set to false if you do not want the profile image to toggle to the normal drawers profile selection
*
* @param enableProfileClick
* @return
* @return this
*/
public MiniDrawer withEnableProfileClick(boolean enableProfileClick) {
this.mEnableProfileClick = enableProfileClick;
return this;
}

private FastAdapter.OnClickListener<IDrawerItem> mOnMiniDrawerItemClickListener;
private OnMiniDrawerItemClickListener mOnMiniDrawerItemClickListener;

/**
* Define an onClickListener for the MiniDrawer item adapter. WARNING: this will overwrite the default behavior
* Define the onMiniDrawerItemClickListener called before any logic in the MiniDrawer is run, allows you to intercept the default behavior
*
* @param onMiniDrawerItemClickListener
* @return
* @return this
*/
public MiniDrawer withOnMiniDrawerItemClickListener(FastAdapter.OnClickListener<IDrawerItem> onMiniDrawerItemClickListener) {
public MiniDrawer withOnMiniDrawerItemClickListener(OnMiniDrawerItemClickListener onMiniDrawerItemClickListener) {
this.mOnMiniDrawerItemClickListener = onMiniDrawerItemClickListener;
return this;
}


private FastAdapter.OnClickListener<IDrawerItem> mOnMiniDrawerItemOnClickListener;

/**
* Define an onClickListener for the MiniDrawer item adapter. WARNING: this will completely overwrite the default behavior
* You may want to check the `OnMiniDrawerItemClickListener` (withOnMiniDrawerItemClickListener) which just hooks into the default behavior
*
* @param onMiniDrawerItemOnClickListener
* @return this
*/
public MiniDrawer withOnMiniDrawerItemOnClickListener(FastAdapter.OnClickListener<IDrawerItem> onMiniDrawerItemOnClickListener) {
this.mOnMiniDrawerItemOnClickListener = onMiniDrawerItemOnClickListener;
return this;
}


private FastAdapter.OnLongClickListener<IDrawerItem> mOnMiniDrawerItemLongClickListener;

/**
Expand Down Expand Up @@ -238,14 +253,24 @@ public ICrossfader getCrossFader() {
return mCrossFader;
}

public FastAdapter.OnClickListener getOnMiniDrawerItemClickListener() {
return mOnMiniDrawerItemClickListener;

/**
* the defined FastAdapter.OnClickListener which completely replaces the original behavior
*
* @return
*/
public FastAdapter.OnClickListener getOnMiniDrawerItemOnClickListener() {
return mOnMiniDrawerItemOnClickListener;
}

/**
* @return
*/
public FastAdapter.OnLongClickListener getOnMiniDrawerItemLongClickListener() {
return mOnMiniDrawerItemLongClickListener;
}


/**
* generates a MiniDrawerItem from a IDrawerItem
*
Expand Down Expand Up @@ -452,13 +477,19 @@ public void createItems() {
}

//listener
if (mOnMiniDrawerItemClickListener != null) {
mAdapter.withOnClickListener(mOnMiniDrawerItemClickListener);
if (mOnMiniDrawerItemOnClickListener != null) {
mAdapter.withOnClickListener(mOnMiniDrawerItemOnClickListener);
} else {
mAdapter.withOnClickListener(new FastAdapter.OnClickListener<IDrawerItem>() {
@Override
public boolean onClick(View v, IAdapter<IDrawerItem> adapter, final IDrawerItem item, final int position) {
int type = getMiniDrawerType(item);

//if a listener is defined and we consume the event return
if (mOnMiniDrawerItemClickListener != null && mOnMiniDrawerItemClickListener.onItemClick(v, position, item, type)) {
return false;
}

if (type == ITEM) {
//fire the onClickListener also if the specific drawerItem is not Selectable
if (item.isSelectable()) {
Expand Down Expand Up @@ -498,4 +529,16 @@ public boolean onClick(View v, IAdapter<IDrawerItem> adapter, final IDrawerItem
private List<IDrawerItem> getDrawerItems() {
return mDrawer.getOriginalDrawerItems() != null ? mDrawer.getOriginalDrawerItems() : mDrawer.getDrawerItems();
}


public interface OnMiniDrawerItemClickListener {
/**
* @param view
* @param position
* @param drawerItem
* @param type either MiniDrawer.PROFILE or MiniDrawer.ITEM
* @return true if the event was consumed
*/
boolean onItemClick(View view, int position, IDrawerItem drawerItem, int type);
}
}

0 comments on commit 7e06953

Please sign in to comment.