[英]How to handle onItemClickListener and OnTOuchListener on the same item in GridView?
First, I would like to apologize for my English. 首先,我想为我的英语道歉。 That's not my native language.
那不是我的母语。
I have a simple gallery in Android. 我在Android中有一个简单的图库。 Images are loaded in
GridView
. 图像加载到
GridView
。 I have problem with onItemClickListener
and onTouchListener
. 我在
onItemClickListener
和onTouchListener
问题。 I have set onItemClickListener
for GridView
and onTouchListener
for childs of GridView
( ImageViews
). 我已
onItemClickListener
为GridView
和onTouchListener
为孩子的GridView
( ImageViews
)。 When I click on the image, it should be shown on the center of phone screen and when I press it, it should zoom-in on the place where my finger was and when I release my finger, it should zoom-out. 当我单击图像时,它应该显示在手机屏幕的中央,当我按它时,它应该放大手指所在的位置,而当我松开手指时,它应该放大。
The problem is that when is set onTouchListener
, only it is fired. 问题在于,当设置
onTouchListener
,仅将其触发。 I would like to recognize when I clicked and when I pressed-released. 我想识别何时单击和何时按下释放。
Thank you for your help. 谢谢您的帮助。
you are looking for checking the value of event.getAction()
which returns MotionEvent
constant: 您正在寻找检查
event.getAction()
的值,该值返回MotionEvent
常量:
gridView.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do something when user touch the screen
//(first touch event-before moving or releasing the finger)
break;
case MotionEvent.ACTION_UP:
//Do something when user release the finger touching the screen.
break;
}
return false;
}
});
You also have ACTION_MOVE
which exist when user move his finger before he released it from the screen (good for dragging etc...) 您还拥有
ACTION_MOVE
,当用户在将手指从屏幕上释放之前移动手指时,该动作就存在(非常适合拖动等)。
EDIT: 编辑:
The problem with onTouch()
is that it only exist for the entire view (Your grid) and you don't have onItemTouch()
. onTouch()
的问题在于它仅存在于整个视图(您的网格)中,而您没有onItemTouch()
。 One solution will be to get the info you need from the item when onItemClick()
happens into a class defined variable (For example the Bitmap
of the picture you clicked on) and then use it to whatever you need in the onTouch()
event. 一种解决方案是,当
onItemClick()
发生在类定义的变量中(例如,您单击的图片的Bitmap
),然后从项目中获取所需的信息,然后将其用于onTouch()
事件中所需的任何内容。 Remember in onTouch()
if your return true at any point it means you already dealt with the touch case and onClick()
won't happen. 请记住在
onTouch()
如果您在任何时候返回true,则意味着您已经处理了触摸盒,并且不会发生onClick()
。 Also - on click calls ONLY when releasing the finger from the screen (means MotionEvent.ACTION_UP
) so you won't have it on the touch event. 同样-仅在将手指从屏幕上释放时(单击时,
MotionEvent.ACTION_UP
),单击时才调用,因此您不会在触摸事件上碰到它。
To solve this you need to either use onItemLongClick()
instead, or set onTouchListener
for each of the items in the grid with custom adapter (in getView()
). 为了解决这个问题,您需要使用
onItemLongClick()
代替,或者使用自定义适配器(在getView()
)为网格中的每个项目设置onTouchListener
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.