gpt4 book ai didi

Android:如何停止调用 View.OnTouchListener ACTION_MOVE

转载 作者:行者123 更新时间:2023-11-29 00:38:41 26 4
gpt4 key购买 nike

我使用 TouchListenerLinearLayout View 进行了简单的滑动操作。

问题是 MotionEvent.ACTION_MOVE 在我触摸和移动时一直被调用。

如何阻止 MotionEvent.ACTION_MOVE 被调用?

v.setOnTouchListener(null) 不好:返回 false 并且不工作。

我希望只调用一次 MotionEvent.ACTION_MOVE

    private View.OnTouchListener touchEvent = new View.OnTouchListener() {
private float tempX;
private final float BASE_VALUE = 100;
private final boolean LEFT = false;
private final boolean RIGHT = true;

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub

if(event.getAction() == MotionEvent.ACTION_DOWN){
tempX = event.getX();
}

if(event.getAction() == MotionEvent.ACTION_MOVE){
if(event.getX() < (tempX + BASE_VALUE)){
changeView(LEFT);
}else if(event.getX() > (tempX + BASE_VALUE)){
changeView(RIGHT);
}
}

它运行不完美。

但是这个实现还不错(这段代码是我的想法)。

代码:

private View.OnTouchListener touchEvent = new View.OnTouchListener() {
private VelocityTracker mVelocityTracker;
private float tempX;
private final int MIN_DISTANCE = 120;
private final int THRESHOLD_VELOCITY = 200;
private final boolean LEFT = false;
private final boolean RIGHT = true;
private MotionEvent cancelEvent = MotionEvent.obtain(100, 100, MotionEvent.ACTION_CANCEL, 0, 0, 0);
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
Log.d(TAG, "Down");
mVelocityTracker = VelocityTracker.obtain();
mVelocityTracker.addMovement(event);
tempX = event.getX();
}
if(event.getAction() == MotionEvent.ACTION_MOVE){
mVelocityTracker.addMovement(event);
mVelocityTracker.computeCurrentVelocity(100);
float velocityX = mVelocityTracker.getXVelocity();
if(tempX - event.getX() > MIN_DISTANCE
&& Math.abs(velocityX) > THRESHOLD_VELOCITY){
Log.d(TAG, "LEFT");
changeView(LEFT);
}else if(event.getX() - tempX > MIN_DISTANCE
&& Math.abs(velocityX) > THRESHOLD_VELOCITY){
Log.d(TAG, "RIGHT");
changeView(RIGHT);
}
v.dispatchTouchEvent(cancelEvent);
}

最佳答案

制作 SWIPELISTENER。

代码。

import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;


public abstract class OnSwipeListener implements OnTouchListener {
public final static int SWIPED_LEFT = 1;
public final static int SWIPED_RIGHT = 2;
public final static int SWIPED_UP = 3;
public final static int SWIPED_DOWN = 4;
public final static int CLICKED = 5;

private int SWIPE_ACTION = 0;
private float firstTouchX = 0;
private float firstTouchY = 0;
private float lastTouchX = 0;
private float lastTouchY = 0;
private int activePointerID;

@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == event.ACTION_DOWN){
final float x = event.getX();
final float y = event.getY();

firstTouchX = x;
firstTouchY = y;

activePointerID = event.getPointerId(0);

v.setBackgroundResource(R.drawable.layoutbutton2);
}

if(event.getAction() == event.ACTION_UP){
float dX = lastTouchX - firstTouchX;
float dY = lastTouchY - firstTouchY;

final int pointerIndex = event.findPointerIndex(activePointerID);

if((firstTouchX + 10) == event.getX(activePointerID) &&
(firstTouchY + 10) == event.getY(activePointerID)){

SWIPE_ACTION = CLICKED;

}else{
if(Math.abs(dX) > Math.abs(dY)){
if(dX > 0){
SWIPE_ACTION = SWIPED_RIGHT;
}else{
SWIPE_ACTION = SWIPED_LEFT;
}
}else{
if(dY > 0){
SWIPE_ACTION = SWIPED_DOWN;
}else{
SWIPE_ACTION = SWIPED_UP;
}
}
}
v.setBackgroundResource(R.drawable.layoutbutton1);
onSwipe(v, event);
}
if(event.getAction() == event.ACTION_MOVE){
final int pointerIndex = event.findPointerIndex(activePointerID);
final float x = event.getX(pointerIndex);
final float y = event.getY(pointerIndex);

lastTouchX = x;
lastTouchY = y;

v.setBackgroundResource(R.drawable.layoutbutton1);
}
if(event.getAction() == event.ACTION_CANCEL){
v.setBackgroundResource(R.drawable.layoutbutton1);
}

return true;

}

public int getSwipeTo(){
return SWIPE_ACTION;
}

abstract public void onSwipe(View v, MotionEvent event);

}

关于Android:如何停止调用 View.OnTouchListener ACTION_MOVE,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10990117/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com