Skip to content

Commit

Permalink
Resolved #428 - Option to disable swipe/drag capabilities when Action…
Browse files Browse the repository at this point in the history
…Mode is activated
  • Loading branch information
davideas committed Aug 23, 2017
1 parent 4fc5158 commit c4733a6
Show file tree
Hide file tree
Showing 2 changed files with 66 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -236,7 +236,9 @@ public void updateContextTitle(int count) {
getString(R.string.action_selected_many, Integer.toString(count)));
}
}
}.withDefaultMode(mode);
}.withDefaultMode(mode)
.disableDragOnActionMode(true)
.disableSwipeOnActionMode(true);
}

private void initializeFragment(Bundle savedInstanceState) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
import eu.davidea.flexibleadapter.utils.Log;

/**
* Helper to coordinates the MULTI selection with FlexibleAdapter.
* Helper to coordinate the MULTI selection with FlexibleAdapter.
*
* @author Davide Steduto
* @since 30/04/2016
Expand All @@ -43,6 +43,8 @@ public class ActionModeHelper implements ActionMode.Callback {
private int defaultMode = Mode.IDLE;
@MenuRes
private int mCabMenu;
private boolean disableSwipe, disableDrag,
longPressDragDisabledByHelper, handleDragDisabledByHelper, swipeDisabledByHelper;
private FlexibleAdapter mAdapter;
private ActionMode.Callback mCallback;
protected ActionMode mActionMode;
Expand Down Expand Up @@ -91,6 +93,32 @@ public final ActionModeHelper withDefaultMode(@Mode int defaultMode) {
return this;
}

/**
* Automatically disables LongPress drag and Handle drag capability when ActionMode is
* activated and enable it again when ActionMode is destroyed.
*
* @param disableDrag true to disable the drag, false to maintain the drag during ActionMode
* @return this object, so it can be chained
* @since 5.0.0-rc3
*/
public final ActionModeHelper disableDragOnActionMode(boolean disableDrag) {
this.disableDrag = disableDrag;
return this;
}

/**
* Automatically disables Swipe capability when ActionMode is activated and enable it again
* when ActionMode is destroyed.
*
* @param disableSwipe true to disable the swipe, false to maintain the swipe during ActionMode
* @return this object, so it can be chained
* @since 5.0.0-rc3
*/
public final ActionModeHelper disableSwipeOnActionMode(boolean disableSwipe) {
this.disableSwipe = disableSwipe;
return this;
}

/**
* @return the current instance of the ActionMode, {@code null} if ActionMode is off.
* @since 5.0.0-b6
Expand Down Expand Up @@ -211,6 +239,8 @@ public boolean onCreateActionMode(ActionMode actionMode, Menu menu) {
Log.d("ActionMode is active!");
// Activate the ActionMode Multi
mAdapter.setMode(Mode.MULTI);
// Disable Swipe and Drag capabilities as per settings
disableSwipeDragCapabilities();
// Notify the provided callback
return mCallback == null || mCallback.onCreateActionMode(actionMode, menu);
}
Expand Down Expand Up @@ -248,6 +278,8 @@ public void onDestroyActionMode(ActionMode actionMode) {
mAdapter.setMode(defaultMode);
mAdapter.clearSelection();
mActionMode = null;
// Re-enable Swipe and Drag capabilities if they were disabled by this helper
enableSwipeDragCapabilities();
// Notify the provided callback
if (mCallback != null) {
mCallback.onDestroyActionMode(actionMode);
Expand All @@ -269,4 +301,34 @@ public boolean destroyActionModeIfCan() {
return false;
}

private void enableSwipeDragCapabilities() {
if (longPressDragDisabledByHelper) {
longPressDragDisabledByHelper = false;
mAdapter.setLongPressDragEnabled(true);
}
if (handleDragDisabledByHelper) {
handleDragDisabledByHelper = false;
mAdapter.setHandleDragEnabled(true);
}
if (swipeDisabledByHelper) {
swipeDisabledByHelper = false;
mAdapter.setSwipeEnabled(true);
}
}

private void disableSwipeDragCapabilities() {
if (disableDrag && mAdapter.isLongPressDragEnabled()) {
longPressDragDisabledByHelper = true;
mAdapter.setLongPressDragEnabled(false);
}
if (disableDrag && mAdapter.isHandleDragEnabled()) {
handleDragDisabledByHelper = true;
mAdapter.setHandleDragEnabled(false);
}
if (disableSwipe && mAdapter.isSwipeEnabled()) {
swipeDisabledByHelper = true;
mAdapter.setSwipeEnabled(false);
}
}

}

0 comments on commit c4733a6

Please sign in to comment.