我有一项活动,我希望能够在其中收听滑动。我的活动内部有一个ScrollView
活动,并且一个很大的活动TextView
已填满了该 ScrollView 的所有区域。为了在我的整个活动上启用滑动,我扩展了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();
}
现在我可以捕获任何扩展 SwipeActivity 的活动rightSwipe()
。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
,除了一件事:
从我在 上滑动大约 10 次,textView
它启动了 3 次,doLongClickAction()
这doSwipeAction()
对用户根本不友好。
我知道我的问题与触摸事件的碰撞有关,但是经过数小时的研究和测试,我无法解决它。你能告诉我我的错误在哪里吗?
(我应该补充一点:当我捕获点击而不是长按 TextView 时,一切正常,但是当用户点击 textview 内的链接时,会启动 textview 的 onClick 事件,然后打开浏览器!)