简体   繁体   English

如何处理GridView中同一项目上的onItemClickListener和OnTOuchListener?

[英]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 . 我在onItemClickListeneronTouchListener问题。 I have set onItemClickListener for GridView and onTouchListener for childs of GridView ( ImageViews ). 我已onItemClickListenerGridViewonTouchListener为孩子的GridViewImageViews )。 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.

相关问题 GridView项目上的OnTouchListener - OnTouchListener on gridview item 如何使用OnItemClickListener更改GridView图像项资源? - How to change GridView image item resource using OnItemClickListener? OnItemClickListener和Custom OnTouchListener - OnItemClickListener and Custom OnTouchListener 如何在gridView上添加OnTouchListener? - How to add an OnTouchListener over a gridView? 当您还具有onTouchListener时,如何触发onItemClickListener? - How do you fire onItemClickListener when you also have onTouchListener? 如果GridView位于PopupWindow中,如何在GridView的OnItemClickListener中关闭PopupWindow - How to close PopupWindow in OnItemClickListener of GridView, if the GridView is in the PopupWindow 如何在 RecyclerView 中处理 onTouchListener - How to handle onTouchListener inside a RecyclerView 如何选择GridView项并在getView中使用onTouchListener?(它们似乎互相抵消了) - How to select GridView item and use onTouchListener in getView?(they seem to cancel each other out) gridview 中的第一项在 AdapterView.OnItemClickListener 中没有改变 - First item in gridview does not change in the AdapterView.OnItemClickListener 如何同时处理 OnTouchListener 事件和 OnClickListener 事件 - How to handle both OnTouchListener event and OnClickListener event
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM