0

First, I would like to apologize for my English. That's not my native language.

I have a simple gallery in Android. Images are loaded in GridView. I have problem with onItemClickListener and onTouchListener. I have set onItemClickListener for GridView and onTouchListener for childs of 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. I would like to recognize when I clicked and when I pressed-released.

Thank you for your help.

4

1 回答 1

0

您正在寻找检查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;
        }
    });

当用户在从屏幕上释放手指之前移动手指时,您也ACTION_MOVE存在这种情况(适合拖动等...)

编辑:

问题onTouch()在于它只存在于整个视图(您的网格)并且您没有onItemTouch(). 一种解决方案是从项目中获取您需要的信息,当它onItemClick()发生在类定义的变量中时(例如,您单击的图片),然后将其用于事件Bitmap中您需要的任何内容。onTouch()请记住,onTouch()如果您在任何时候返回 true,则意味着您已经处理了触摸案例并且onClick()不会发生。另外 - 仅在从屏幕上释放手指时单击调用(意味着MotionEvent.ACTION_UP),因此您不会在触摸事件中使用它。

要解决此问题,您需要改为使用onItemLongClick(),或onTouchListener使用自定义适配器(in 中getView())为网格中的每个项目设置。

于 2013-04-28T15:48:41.390 回答