Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/neo4j/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 在TextView上滑动/投掷也会随机运行其onLongClick_Android_Textview_Swipe_Touch Event_Onlongclicklistener - Fatal编程技术网

Android 在TextView上滑动/投掷也会随机运行其onLongClick

Android 在TextView上滑动/投掷也会随机运行其onLongClick,android,textview,swipe,touch-event,onlongclicklistener,Android,Textview,Swipe,Touch Event,Onlongclicklistener,我有一个活动,我想能够听到它的滑动。在我的活动中有一个ScrollView,一个大的TextView占据了该滚动视图的所有区域。为了在我的整个活动中启用滑动,我扩展了Activity类并定义了SwipeActivity,如下所示: public abstract class SwipeActivity extends Activity { private static final int SWIPE_MIN_DISTANCE = 120; private static final int SW

我有一个活动,我想能够听到它的滑动。在我的活动中有一个
ScrollView
,一个大的
TextView
占据了该滚动视图的所有区域。为了在我的整个活动中启用滑动,我扩展了
Activity
类并定义了
SwipeActivity
,如下所示:

public abstract class SwipeActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private int SWIPE_THRESHOLD_VELOCITY;
private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(new SwipeDetector());
    ViewConfiguration configuration = ViewConfiguration.get(this);
    SWIPE_THRESHOLD_VELOCITY = configuration.getScaledMinimumFlingVelocity();
}

private class SwipeDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        // Check movement along the Y-axis. If it exceeds
        // SWIPE_MAX_OFF_PATH,
        // then dismiss the swipe.
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        // Swipe from right to left.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
        // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            leftSwipe();
            return true;
        }

        // Swipe from left to right.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
        // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            rightSwipe();
            return true;
        }

        return false;
    }
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TouchEvent dispatcher.
    if (gestureDetector != null) {
        if (gestureDetector.onTouchEvent(ev))
            // If the gestureDetector handles the event, a swipe has been
            // executed and no more needs to be done.
            return true;
    }
    return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

protected abstract void rightSwipe();

protected abstract void leftSwipe();
}
public class ContentActivity extends SwipeActivity implements OnLongClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Here I add a TextView programmatically to my Vertical ScrollView and set this activity to listen to its long clicks (touches)
        ScrollView scrollView = (ScrollView) findViewById(R.id.content_scroll);
        TextView textView = new TextView(ContentActivity.this);
        textView.setText("Some Really Long Text!");
        textView.setOnLongClickListener(this);
        scrollView.addView(textView);
    }      

    @Override
    public boolean onLongClick(View v) {
        return doLongClickAction();
    }

    @Override
    public void rightSwipe() {
        doSwipeAction();
    }

    @Override
    public void leftSwipe() {
        doSwipeAction();
    }

}
现在我可以在任何扩展SwipeActivity的活动上捕获
rightSwipe()
leftSwipe()
。因此,我宣布我的主要活动如下:

public abstract class SwipeActivity extends Activity {

private static final int SWIPE_MIN_DISTANCE = 120;
private static final int SWIPE_MAX_OFF_PATH = 250;
private int SWIPE_THRESHOLD_VELOCITY;
private GestureDetector gestureDetector;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    gestureDetector = new GestureDetector(new SwipeDetector());
    ViewConfiguration configuration = ViewConfiguration.get(this);
    SWIPE_THRESHOLD_VELOCITY = configuration.getScaledMinimumFlingVelocity();
}

private class SwipeDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
            float velocityY) {

        // Check movement along the Y-axis. If it exceeds
        // SWIPE_MAX_OFF_PATH,
        // then dismiss the swipe.
        if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
            return false;

        // Swipe from right to left.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
        // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            leftSwipe();
            return true;
        }

        // Swipe from left to right.
        // The swipe needs to exceed a certain distance (SWIPE_MIN_DISTANCE)
        // and a certain velocity (SWIPE_THRESHOLD_VELOCITY).
        if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE
                && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
            rightSwipe();
            return true;
        }

        return false;
    }
}

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    // TouchEvent dispatcher.
    if (gestureDetector != null) {
        if (gestureDetector.onTouchEvent(ev))
            // If the gestureDetector handles the event, a swipe has been
            // executed and no more needs to be done.
            return true;
    }
    return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}

protected abstract void rightSwipe();

protected abstract void leftSwipe();
}
public class ContentActivity extends SwipeActivity implements OnLongClickListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        // Here I add a TextView programmatically to my Vertical ScrollView and set this activity to listen to its long clicks (touches)
        ScrollView scrollView = (ScrollView) findViewById(R.id.content_scroll);
        TextView textView = new TextView(ContentActivity.this);
        textView.setText("Some Really Long Text!");
        textView.setOnLongClickListener(this);
        scrollView.addView(textView);
    }      

    @Override
    public boolean onLongClick(View v) {
        return doLongClickAction();
    }

    @Override
    public void rightSwipe() {
        doSwipeAction();
    }

    @Override
    public void leftSwipe() {
        doSwipeAction();
    }

}
现在,所有内容都显示良好,垂直
scrollView
可以正确滚动,滑动操作和长时间单击可以在
textView
上工作,除了一件事:

我在
textView
上滑动了大约10次,3次它同时启动
doLongClickAction()
DosweepAction()
,这对用户来说一点都不友好。

我知道我的问题与触摸事件的碰撞有关,但经过数小时的研究和测试,我无法解决它。你能告诉我我的错在哪里吗

(我应该补充一点:当我在TextView上捕获点击而不是长时间点击时,一切正常,但当用户单击TextView内的链接时,TextView的onClick事件启动,然后浏览器打开!)