我在列表视图项滑动时正在显示图像。要实现这一点,我正在使用帮助类(不是库)
帮助程序类代码
SwipeDetector
import android.annotation.SuppressLint;
import android.view.MotionEvent;
import android.view.View;
public class SwipeDetector implements View.OnTouchListener {
public static enum Action {
LR, // Left to Right
RL, // Right to Left
TB, // Top to bottom
BT, // Bottom to Top
None // when no action was detected
}
//private static final String logTag = "SwipeDetector";
private static final int MIN_DISTANCE = 100;
private float downX, downY, upX, upY;
private Action mSwipeDetected = Action.None;
public boolean swipeDetected() {
return mSwipeDetected != Action.None;
}
public Action getAction() {
return mSwipeDetected;
}
@SuppressLint("ClickableViewAccessibility") public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN: {
downX = event.getX();
downY = event.getY();
mSwipeDetected = Action.None;
return false; // allow other events like Click to be processed
}
case MotionEvent.ACTION_MOVE: {
upX = event.getX();
upY = event.getY();
float deltaX = downX - upX;
float deltaY = downY - upY;
// horizontal swipe detection
if (Math.abs(deltaX) > MIN_DISTANCE) {
// left or right
if (deltaX < 0) {
//Logger.show(Log.INFO,logTag, "Swipe Left to Right");
mSwipeDetected = Action.LR;
return true;
}
if (deltaX > 0) {
//Logger.show(Log.INFO,logTag, "Swipe Right to Left");
mSwipeDetected = Action.RL;
return true;
}
} else
// vertical swipe detection
if (Math.abs(deltaY) > MIN_DISTANCE) {
// top or down
if (deltaY < 0) {
//Logger.show(Log.INFO,logTag, "Swipe Top to Bottom");
mSwipeDetected = Action.TB;
return false;
}
if (deltaY > 0) {
//Logger.show(Log.INFO,logTag, "Swipe Bottom to Top");
mSwipeDetected = Action.BT;
return false;
}
}
return true;
}
}
return false;
}
}
为此,我隐藏了android:visibility="gone"
等布局中的视图,当用户从右向左滑动(了解请参阅此link)并隐藏时,我正在显示图像(View.VISIBLE) View.GONE)从左向右滑动时。对于这个隐藏(View.GONE)我正在设置这样的动画
从左向右滑动
TranslateAnimation animate = new TranslateAnimation(0,ivAdptDelete.getWidth(),0,0);
animate.setDuration(500);
animate.setFillAfter(true);
ivAdptDelete.startAnimation(animate);
ivAdptDelete.setVisibility(View.GONE);
Runnable buttonClickable = new Runnable() {
@Override
public void run() {
ivAdptDelete.clearAnimation();
}
};
Handler pdCanceller = new Handler();
pdCanceller.postDelayed(buttonClickable, 500);
从右向左滑动
ivAdptDelete.setVisibility(View.VISIBLE);
我第一次从左向右滑动意味着什么都没有显示,因为我的视图被隐藏(这是正确的过程)。如果我从右向左滑动从左到右一切都很好。完成此过程后,如果我从左向右滑动意味着动画再次重复。我怀疑是当我从左向右滑动ivAdptDelete.setVisibility(View.GONE);
时隐藏视图,但是当我从左向右滑动时它再次显示动画。因此,任何人都可以在第二次从左向右滑动时限制动画
找我的代码,请查看link