简体   繁体   English

在Android的ListView中滑动时,视图前后移动

[英]View moves back and forth when swiping in listview in android

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 这是我在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 在ACTION_MOVE中打印deltaX的值

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

I have reproduced the problem, and this seems to work: instead of setTranslationX(deltaX-mSwipingSlop) , use setX(getX() + deltaX-mSwipingSlop) . 我已经重现了这个问题,这似乎行得通:代替setTranslationX(deltaX-mSwipingSlop) ,请使用setX(getX() + deltaX-mSwipingSlop)

Using getRawX() instead of getX() works as well, but needs some extra calculations to accommodate for second gestures. 使用getRawX()代替getX()也可以,但是需要一些额外的计算来适应第二个手势。

I am not entirely sure why this behavior occurs though. 我不完全确定为什么会发生这种现象。

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

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