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,远程控制或游戏控制器作为输入设备时,此错误才会影响您。 正常的触摸屏事件已得到正确处理。
ext itemclick