Android Core

Resolving the dual ItemClick conundrum for the Android Gallery View

Yes, the Gallery View is deprecated, however currently there really isn’t an out of the box solution to what the gallery view provides, which is a center locked horizontal scrolling list of items. The Twoway-view project has some potential in addressing this need, but it needs time to mature a bit.

There are many issues with the Gallery view, but one of the most significant is if you need the ability to show context menus for an item. If you are using it with a D-Pad (say in an Android TV application), and you long press on an item using D-PAD OK, or BUTTON-A, you’ll get both ItemLongClick and ItemClick events fired by the gallery. This has been a long standing bug within the app, an has plagued me for years. I finally tracked it down and created the following gist that can be applied to a custom version of the Gallery.

 /**
  * 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);
	}

Basically, what was happening is that the onKeyUp event that fired the ItemClick event never checked to see if the dispatchLongPress event had already handled the event. So it was always firing the ItemClick event. The gist makes this small change, and actually eliminates numerous work arounds that had to be done to get context menus to show with the gallery.

This bug only affects you if you needed to use a D-PAD, Remote Control, or Game Controller as an input device. Normal touch screen events are handled correctly.

Subscribe
Notify of
guest

This site uses Akismet to reduce spam. Learn how your comment data is processed.

0 Comments
Inline Feedbacks
View all comments
Back to top button