【发布时间】:2014-07-26 16:50:08
【问题描述】:
我想使用下面的代码从FrameLayout 中取消TextView。
目前,当方法触发时,我正在获取日志,这很棒!
但是如何执行滑动以关闭?
SwipeDismissTouchListener.java
public class SwipeDismissTouchListener implements View.OnTouchListener {
static final String logTag = "ActivitySwipeDetector";
private Activity activity;
static final int MIN_DISTANCE = 100;
private float downX, upX;
public SwipeDismissTouchListener(GameActivity gameActivity) {
activity = gameActivity;
}
public void onRightToLeftSwipe() {
Log.i(logTag, "RightToLeftSwipe!");
Toast.makeText(activity, "RightToLeftSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}
public void onLeftToRightSwipe() {
Log.i(logTag, "LeftToRightSwipe!");
Toast.makeText(activity, "LeftToRightSwipe", Toast.LENGTH_SHORT).show();
// activity.doSomething();
}
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
return true;
}
case MotionEvent.ACTION_UP: {
upX = event.getX();
float deltaX = downX - upX;
if (Math.abs(deltaX) > MIN_DISTANCE) {
if (deltaX < 0) {
this.onLeftToRightSwipe();
return true;
}
if (deltaX > 0) {
this.onRightToLeftSwipe();
return true;
}
} else {
Log.i(logTag, "Swipe was only " + Math.abs(deltaX) + " long horizontally, need at least " + MIN_DISTANCE);
// return false; // We don't consume the event
}
return false; // no swipe horizontally and no swipe vertically
}// case MotionEvent.ACTION_UP:
}
return false;
}
}
MyActivity.java
SwipeDismissTouchListener swipeListener = new SwipeDismissTouchListener(this);
frameLayout.setOnTouchListener(swipeListener);
【问题讨论】:
标签: android textview ontouchlistener android-framelayout