Created
August 19, 2014 14:29
-
-
Save kingargyle/d41d043e9f3423e8b2a1 to your computer and use it in GitHub Desktop.
Fix ItemLongClick and ItemClick both firing in Gallery widget
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* This change should work in both the original Gallery code as well as in the vairous open source | |
* extensions and variations. | |
* | |
* Find the displatchLongPress method in the Gallery class file. and update it to the following. | |
* | |
* Make sure that longPressHandled is a field available to all the other methods. | |
* | |
*/ | |
boolean longPressHandled = false; | |
private boolean dispatchLongPress(View view, int position, long id) { | |
longPressHandled = false; | |
if (mOnItemLongClickListener != null) { | |
longPressHandled = mOnItemLongClickListener.onItemLongClick(this, | |
mDownTouchView, mDownTouchPosition, id); | |
} | |
if (!longPressHandled) { | |
mContextMenuInfo = new AdapterContextMenuInfo(view, position, id); | |
longPressHandled = super.showContextMenuForChild(this); | |
} | |
if (longPressHandled) { | |
performHapticFeedback(HapticFeedbackConstants.LONG_PRESS); | |
} | |
return longPressHandled; | |
} | |
/** | |
* Find the onKeyUp method and change it to the following. | |
* | |
* Since dispatchLongPress is fired before the onKeyUp event, we have | |
* to check to see if the onItemLongClickListener handled the event. | |
* If so do not performItemClick, and always reset the longPressHandled to false. | |
* | |
*/ | |
@Override | |
public boolean onKeyUp(int keyCode, KeyEvent event) { | |
switch (keyCode) { | |
case KeyEvent.KEYCODE_DPAD_CENTER: | |
case KeyEvent.KEYCODE_ENTER: { | |
if (mReceivedInvokeKeyDown) { | |
if (mItemCount > 0) { | |
dispatchPress(mSelectedChild); | |
postDelayed(new Runnable() { | |
@Override | |
public void run() { | |
dispatchUnpress(); | |
} | |
}, ViewConfiguration.getPressedStateDuration()); | |
int selectedIndex = mSelectedPosition - mFirstPosition; | |
if (!longPressHandled) { | |
performItemClick(getChildAt(selectedIndex), | |
mSelectedPosition, | |
mAdapter.getItemId(mSelectedPosition)); | |
} | |
} | |
} | |
// Clear the flag | |
mReceivedInvokeKeyDown = false; | |
longPressHandled = false; | |
return true; | |
} | |
} | |
return super.onKeyUp(keyCode, event); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment