我试图将 ListViewRemovalAnimation ( http://developer.android.com/shareables/devbytes/ListViewRemovalAnimation.zip ) 与 Cell Dragging & Rearranging ( http://developer.android.com/shareables/devbytes/ListViewDraggingAnimation.zip )结合起来
当用户在 onTouch 中按住视图 x 秒并因此调用 LongClick 时,我试图返回 false。
但是滑动到删除似乎工作得很好,但我似乎找不到在需要时调用 onLongClickListener 的方法。我尝试获取用户第一次按下视图和当前时间之间的时间差,但这似乎不起作用。我还考虑过使用带有 runable 的 Handler,但我想不出一种方法来完成我想要的。
我有一种感觉,我从错误的角度来处理这个问题,但是任何通过拖动和重新排列列表视图来滑动删除的帮助将不胜感激:)
这是 OnTouch 的代码:
public boolean onTouch(final View v, MotionEvent event) {
time = event.getDownTime();
if (mSwipeSlop < 0) {
mSwipeSlop = ViewConfiguration.get(ListViewDraggingAnimation.this).
getScaledTouchSlop();
}
if(SystemClock.uptimeMillis() - time > 2000) {
return false;
}
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
if (mAnimating) {
// Multi-item swipes not handled
return false;
}
mItemPressed = true;
mDownX = event.getX();
break;
case MotionEvent.ACTION_CANCEL:
setSwipePosition(v, 0);
mItemPressed = false;
break;
case MotionEvent.ACTION_MOVE:
{
ifNameTaken.setText("debug69");
if (mAnimating) {
return false;
}
float x = event.getX();
if (isRuntimePostGingerbread()) {
x += v.getTranslationX();
}
float deltaX = x - mDownX;
float deltaXAbs = Math.abs(deltaX);
if (!mSwiping) {
if (deltaXAbs > mSwipeSlop) {
mSwiping = true;
listView.requestDisallowInterceptTouchEvent(true);
}
}
if (mSwiping) {
setSwipePosition(v, deltaX);
}
}
break;
case MotionEvent.ACTION_UP:
{
if (mAnimating) {
return false;
}
// User let go - figure out whether to animate the view out, or back into place
if (mSwiping) {
float x = event.getX();
if (isRuntimePostGingerbread()) {
x += v.getTranslationX();
}
float deltaX = x - mDownX;
float deltaXAbs = Math.abs(deltaX);
float fractionCovered;
float endX;
final boolean remove;
if (deltaXAbs > v.getWidth() / 4) {
// Greater than a quarter of the width - animate it out
fractionCovered = deltaXAbs / v.getWidth();
endX = deltaX < 0 ? -v.getWidth() : v.getWidth();
remove = true;
} else {
// Not far enough - animate it back
fractionCovered = 1 - (deltaXAbs / v.getWidth());
endX = 0;
remove = false;
}
// Animate position and alpha
long duration = (int) ((1 - fractionCovered) * SWIPE_DURATION);
animateSwipe(v, endX, duration, remove);
} else {
mItemPressed = false;
}
}
break;
default:
return false;
}
return true;
}
我也没有编辑任何 ListViewDraggingAnimation OnLongClick 代码。
编辑:我尝试将 SystemClock.uptimeMillis() - event.getDownTime() > 2000 之间的比较放在开关之外,认为 Action_Down 仅在用户仅按下时执行,但仍然没有返回 false。我认为问题可能出在逻辑比较本身