ext itemclick_解决Android Gallery视图的双重ItemClick难题

ext itemclick

ext itemclick

是的,不建议使用“图库视图”,但是目前,对于“图库”视图所提供的功能,实际上还没有开箱即用的解决方案,这是一个中心锁定的水平滚动项目列表。 “双向视图”项目在满足此需求方面具有一定潜力,但需要一些时间才能成熟。

“图库”视图有很多问题,但最重要的问题之一是是否需要显示项目的上下文菜单的功能。 如果您将其与D-Pad配合使用(例如在Android TV应用程序中),并且使用D-PAD OK或BUTTON-A长按某个项目,则画廊会触发ItemLongClick和ItemClick事件。 这是应用程序中长期存在的错误,困扰了我很多年。 我终于找到了它,并创建了以下要点,这些要点可以应用于自定义版本的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);
	}

基本上,发生的事情是触发ItemClick事件的onKeyUp事件从未检查过是否查看dispatchLongPress事件是否已经处理了该事件。 因此,它始终会触发ItemClick事件。 要点进行了微小的更改,并实际上消除了使上下文菜单与图库一起显示所必须进行的许多变通方法。

仅当您需要使用D-PAD,远程控制或游戏控制器作为输入设备时,此错误才会影响您。 正常的触摸屏事件已得到正确处理。

翻译自: https://www.javacodegeeks.com/2014/08/resolving-the-dual-itemclick-conundrum-for-the-android-gallery-view.html

ext itemclick

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值