简体   繁体   English

Android OnTouch操作无法正常进行

[英]Android OnTouch action up not doesn't fire with sweeping

I have set an OnTouchListener on a imageview in my gridview, so everytime it's clicked ACTION DOWN fires and i change the background of the image of a clicked version of it, so it emulates a click and on ACTION UP i return the old 我在gridview的imageview上设置了OnTouchListener,因此每次单击时都会触发ACTION DOWN,并且更改其单击版本的图像的背景,因此它模拟单击,并在ACTION UP上返回旧的

Everything works fine when I click it but when i slide it slightly out of bounds the UP event doesn't happen and the image stays the same. 单击它时一切正常,但是当我将其稍微滑出界限时,不会发生UP事件,并且图像保持不变。 That seems logical to me since I am not releasing the touch but I am sliding it outside of the imageview. 在我看来这是合乎逻辑的,因为我没有释放触摸,而是将其滑到了imageview之外。 I tried to limit the background change to happen only if the click is inside the bounds of the view, but nothing worked, this is my touch listener event : 我试图将背景更改限制为仅在单击位于视图范围内时才发生,但没有任何效果,这是我的触摸监听器事件:

imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                    // Log.i("IMAGE", "motion event: " + imageView.toString());
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                       ((ImageView) view).setImageResource(R.drawable.test01);
                       Log.i("IMAGE", "motion event: " + "Action down Inside");

                        //imageView.setBackgroundResource(0);
                        Log.i("IMAGE", "motion event: " + "Action down");
                        return true;
                    case MotionEvent.ACTION_UP:
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                        ((ImageView)view).setImageResource(mThumbIds[position]);
                        Log.i("IMAGE", "motion event: " + "Action up");
                        return true;

                }
                return false;
            }

        });

and this is another version in which i tried something else : 这是另一个我尝试过的版本:

imageView.setOnTouchListener(new OnSwipeTouchListener(mContext) {
            @Override
            public boolean onTouch(View view, MotionEvent motionEvent) {
                    // Log.i("IMAGE", "motion event: " + imageView.toString());
                switch (motionEvent.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        Log.i("IMAGE", "xC: " + (int)motionEvent.getX() + "   yC:" + (int)motionEvent.getX());
                        if(isViewContains(view, (int)motionEvent.getX(), (int)motionEvent.getY())) {
                            ((ImageView) view).setImageResource(R.drawable.test01);
                            Log.i("IMAGE", "motion event: " + "Action down Inside");
                        }
                        //imageView.setBackgroundResource(0);
                        Log.i("IMAGE", "motion event: " + "Action down");
                        return true;
                    case MotionEvent.ACTION_UP:
                        try {
                            Thread.sleep(100);
                        } catch (InterruptedException e) {

                            e.printStackTrace();
                        }
                        ((ImageView)view).setImageResource(mThumbIds[position]);
                        Log.i("IMAGE", "motion event: " + "Action up");
                        return true;
                }
                return true;
            }
        });

this is the function that I am using 这是我正在使用的功能

private boolean isViewContains(View view, int rx, int ry) {
        int[] l = new int[2];
        view.getLocationOnScreen(l);
        Rect rect = new Rect(view.getLeft(), view.getTop(), view.getRight(), view.getBottom());
        Log.i("IMAGE", "x: " + rx + "  y:" + ry);
        if(rect.contains(view.getLeft()+ rx, view.getTop() + ry)) {
                Log.i("IMAGE", "contains");
        }
        return rect.contains(view.getLeft()+ rx, view.getTop() + ry);
}

Do you have any hints or suggestions ? 您有什么提示或建议吗? What I want to is no matter what I do swipe or click the original image to return. 无论我要刷卡还是单击原始图像返回,我都想要。

尝试MotionEvent.ACTION_CANCELonTouch(查看视图,MotionEvent motionEvent)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM