View moves back and forth when swiping in listview in android

非 Y 不嫁゛ 提交于 2019-12-11 09:56:22

问题


I have a list view with each list item containing a button (with a text on it) and an image. I am trying to implement swiping a swiping effect when the user touches the listitem and moves his finger to the left or right. This is my code in onIntercetTouchEvent

case MotionEvent.ACTION_DOWN:
                mDownX=motionEvent.getX();
                Log.i(TAG, "onInterceptTouchEvent.ACTION_DOWN");
                break;
            case MotionEvent.ACTION_MOVE:
                float deltaX=motionEvent.getX()-mDownX;
                int mSwipingSlop=deltaX>0?mSlop:-mSlop;
                this.setTranslationX(deltaX-mSwipingSlop);
                Log.i(TAG, "onInterceptTouchEvent.ACTION_MOVE");
                break;

It works. But the problem is that there is a blurring effect when the swiping is happening. Even though I am swiping in one direction, button in the view seems to move back and forth very fast and thus giving a blurring effect.

Can anyone tell what is the problem here and a possible solution?

Printing value of deltaX in for ACTION_MOVE

onInterceptTouchEvent.ACTION_DOWN
\ onInterceptTouchEvent.ACTION_MOVE---13.481277
onInterceptTouchEvent.ACTION_MOVE---4.9546204
\onInterceptTouchEvent.ACTION_MOVE---16.477112
onInterceptTouchEvent.ACTION_MOVE---30.629227
onInterceptTouchEvent.ACTION_MOVE---33.33777
onInterceptTouchEvent.ACTION_MOVE---53.111282
onInterceptTouchEvent.ACTION_MOVE---53.15001
onInterceptTouchEvent.ACTION_MOVE---74.20006
onInterceptTouchEvent.ACTION_MOVE---75.70354
onInterceptTouchEvent.ACTION_MOVE---94.25169
onInterceptTouchEvent.ACTION_MOVE---95.33977
onInterceptTouchEvent.ACTION_MOVE---116.58623
onInterceptTouchEvent.ACTION_MOVE---117.084274
onInterceptTouchEvent.ACTION_MOVE---140.37877
onInterceptTouchEvent.ACTION_MOVE---137.01903
onInterceptTouchEvent.ACTION_MOVE---157.82243
onInterceptTouchEvent.ACTION_MOVE---152.6262
onInterceptTouchEvent.ACTION_MOVE---172.87059
onInterceptTouchEvent.ACTION_MOVE---165.39714
onInterceptTouchEvent.ACTION_MOVE---182.45793
onInterceptTouchEvent.ACTION_MOVE---174.23326
onInterceptTouchEvent.ACTION_MOVE---190.94212
onInterceptTouchEvent.ACTION_MOVE---182.86662
onInterceptTouchEvent.ACTION_MOVE---196.75847

回答1:


I have reproduced the problem, and this seems to work: instead of setTranslationX(deltaX-mSwipingSlop), use setX(getX() + deltaX-mSwipingSlop).

Using getRawX() instead of getX() works as well, but needs some extra calculations to accommodate for second gestures.

I am not entirely sure why this behavior occurs though.



来源:https://stackoverflow.com/questions/25271128/view-moves-back-and-forth-when-swiping-in-listview-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!