我想使用以下代码从TextView
中删除FrameLayout
。
目前,当方法触发时,我正在获取日志,这太棒了!
但是如何执行滑动才能解除?
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);
答案 0 :(得分:1)
Bar链接是一个很好的起点。
您通常要做的是设置TextView的translateX和alpha属性... Roman Nurik还添加了一个速度跟踪器来检测&#34;滑动手势&#34;并开始一个动画,它也将为translateX和alpha属性设置动画
在SwipeDismiss之后,您只需调用framelayout.removeView(textView)
您也可以考虑覆盖FrameLayout的onTouch方法(FrameLayout的子类),如下所述:
http://developer.android.com/training/gestures/viewgroup.html