|
|
@ -16,10 +16,6 @@ |
|
|
|
|
|
|
|
|
|
|
|
package org.floens.chan.ui; |
|
|
|
package org.floens.chan.ui; |
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import android.animation.Animator; |
|
|
|
import android.animation.Animator; |
|
|
|
import android.animation.AnimatorListenerAdapter; |
|
|
|
import android.animation.AnimatorListenerAdapter; |
|
|
|
import android.animation.ValueAnimator; |
|
|
|
import android.animation.ValueAnimator; |
|
|
@ -32,6 +28,10 @@ import android.view.ViewGroup; |
|
|
|
import android.widget.AbsListView; |
|
|
|
import android.widget.AbsListView; |
|
|
|
import android.widget.ListView; |
|
|
|
import android.widget.ListView; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList; |
|
|
|
|
|
|
|
import java.util.Collections; |
|
|
|
|
|
|
|
import java.util.List; |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
/** |
|
|
|
* A {@link android.view.View.OnTouchListener} that makes the list items in a |
|
|
|
* A {@link android.view.View.OnTouchListener} that makes the list items in a |
|
|
|
* {@link ListView} dismissable. {@link ListView} is given special treatment |
|
|
|
* {@link ListView} dismissable. {@link ListView} is given special treatment |
|
|
@ -109,11 +109,9 @@ public class SwipeDismissListViewTouchListener implements View.OnTouchListener { |
|
|
|
* Called when the user has indicated they she would like to dismiss one |
|
|
|
* Called when the user has indicated they she would like to dismiss one |
|
|
|
* or more list item positions. |
|
|
|
* or more list item positions. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param listView |
|
|
|
* @param listView The originating {@link ListView}. |
|
|
|
* The originating {@link ListView}. |
|
|
|
* @param reverseSortedPositions An array of positions to dismiss, sorted in descending |
|
|
|
* @param reverseSortedPositions |
|
|
|
* order for convenience. |
|
|
|
* An array of positions to dismiss, sorted in descending |
|
|
|
|
|
|
|
* order for convenience. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
void onDismiss(ListView listView, int[] reverseSortedPositions); |
|
|
|
void onDismiss(ListView listView, int[] reverseSortedPositions); |
|
|
|
} |
|
|
|
} |
|
|
@ -121,11 +119,9 @@ public class SwipeDismissListViewTouchListener implements View.OnTouchListener { |
|
|
|
/** |
|
|
|
/** |
|
|
|
* Constructs a new swipe-to-dismiss touch listener for the given list view. |
|
|
|
* Constructs a new swipe-to-dismiss touch listener for the given list view. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param listView |
|
|
|
* @param listView The list view whose items should be dismissable. |
|
|
|
* The list view whose items should be dismissable. |
|
|
|
* @param callbacks The callback to trigger when the user has indicated that she |
|
|
|
* @param callbacks |
|
|
|
* would like to dismiss one or more list items. |
|
|
|
* The callback to trigger when the user has indicated that she |
|
|
|
|
|
|
|
* would like to dismiss one or more list items. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public SwipeDismissListViewTouchListener(ListView listView, DismissCallbacks callbacks) { |
|
|
|
public SwipeDismissListViewTouchListener(ListView listView, DismissCallbacks callbacks) { |
|
|
|
ViewConfiguration vc = ViewConfiguration.get(listView.getContext()); |
|
|
|
ViewConfiguration vc = ViewConfiguration.get(listView.getContext()); |
|
|
@ -149,8 +145,7 @@ public class SwipeDismissListViewTouchListener implements View.OnTouchListener { |
|
|
|
* Enables or disables (pauses or resumes) watching for swipe-to-dismiss |
|
|
|
* Enables or disables (pauses or resumes) watching for swipe-to-dismiss |
|
|
|
* gestures. |
|
|
|
* gestures. |
|
|
|
* |
|
|
|
* |
|
|
|
* @param enabled |
|
|
|
* @param enabled Whether or not to watch for gestures. |
|
|
|
* Whether or not to watch for gestures. |
|
|
|
|
|
|
|
*/ |
|
|
|
*/ |
|
|
|
public void setEnabled(boolean enabled) { |
|
|
|
public void setEnabled(boolean enabled) { |
|
|
|
mPaused = !enabled; |
|
|
|
mPaused = !enabled; |
|
|
@ -195,126 +190,126 @@ public class SwipeDismissListViewTouchListener implements View.OnTouchListener { |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
switch (motionEvent.getActionMasked()) { |
|
|
|
switch (motionEvent.getActionMasked()) { |
|
|
|
case MotionEvent.ACTION_DOWN: { |
|
|
|
case MotionEvent.ACTION_DOWN: { |
|
|
|
if (mPaused) { |
|
|
|
if (mPaused) { |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// TODO: ensure this is a finger, and set a flag
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
// Find the child view that was touched (perform a hit test)
|
|
|
|
|
|
|
|
Rect rect = new Rect(); |
|
|
|
|
|
|
|
int childCount = mListView.getChildCount(); |
|
|
|
|
|
|
|
int[] listViewCoords = new int[2]; |
|
|
|
|
|
|
|
mListView.getLocationOnScreen(listViewCoords); |
|
|
|
|
|
|
|
int x = (int) motionEvent.getRawX() - listViewCoords[0]; |
|
|
|
|
|
|
|
int y = (int) motionEvent.getRawY() - listViewCoords[1]; |
|
|
|
|
|
|
|
View child; |
|
|
|
|
|
|
|
for (int i = 0; i < childCount; i++) { |
|
|
|
|
|
|
|
child = mListView.getChildAt(i); |
|
|
|
|
|
|
|
child.getHitRect(rect); |
|
|
|
|
|
|
|
if (rect.contains(x, y)) { |
|
|
|
|
|
|
|
mDownView = child; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mDownView != null) { |
|
|
|
|
|
|
|
mDownX = motionEvent.getRawX(); |
|
|
|
|
|
|
|
mDownPosition = mListView.getPositionForView(mDownView); |
|
|
|
|
|
|
|
if (mCallbacks.canDismiss(mDownPosition)) { |
|
|
|
|
|
|
|
mVelocityTracker = VelocityTracker.obtain(); |
|
|
|
|
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
mDownView = null; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
view.onTouchEvent(motionEvent); |
|
|
|
|
|
|
|
return true; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
// TODO: ensure this is a finger, and set a flag
|
|
|
|
case MotionEvent.ACTION_UP: { |
|
|
|
|
|
|
|
if (mVelocityTracker == null) { |
|
|
|
// Find the child view that was touched (perform a hit test)
|
|
|
|
|
|
|
|
Rect rect = new Rect(); |
|
|
|
|
|
|
|
int childCount = mListView.getChildCount(); |
|
|
|
|
|
|
|
int[] listViewCoords = new int[2]; |
|
|
|
|
|
|
|
mListView.getLocationOnScreen(listViewCoords); |
|
|
|
|
|
|
|
int x = (int) motionEvent.getRawX() - listViewCoords[0]; |
|
|
|
|
|
|
|
int y = (int) motionEvent.getRawY() - listViewCoords[1]; |
|
|
|
|
|
|
|
View child; |
|
|
|
|
|
|
|
for (int i = 0; i < childCount; i++) { |
|
|
|
|
|
|
|
child = mListView.getChildAt(i); |
|
|
|
|
|
|
|
child.getHitRect(rect); |
|
|
|
|
|
|
|
if (rect.contains(x, y)) { |
|
|
|
|
|
|
|
mDownView = child; |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if (mDownView != null) { |
|
|
|
float deltaX = motionEvent.getRawX() - mDownX; |
|
|
|
mDownX = motionEvent.getRawX(); |
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
mDownPosition = mListView.getPositionForView(mDownView); |
|
|
|
mVelocityTracker.computeCurrentVelocity(1000); |
|
|
|
if (mCallbacks.canDismiss(mDownPosition)) { |
|
|
|
float velocityX = mVelocityTracker.getXVelocity(); |
|
|
|
mVelocityTracker = VelocityTracker.obtain(); |
|
|
|
float absVelocityX = Math.abs(velocityX); |
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); |
|
|
|
|
|
|
|
boolean dismiss = false; |
|
|
|
|
|
|
|
boolean dismissRight = false; |
|
|
|
|
|
|
|
if (Math.abs(deltaX) > mViewWidth / 2) { |
|
|
|
|
|
|
|
dismiss = true; |
|
|
|
|
|
|
|
dismissRight = deltaX > 0; |
|
|
|
|
|
|
|
} else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity |
|
|
|
|
|
|
|
&& absVelocityY < absVelocityX) { |
|
|
|
|
|
|
|
// dismiss only if flinging in the same direction as dragging
|
|
|
|
|
|
|
|
dismiss = (velocityX < 0) == (deltaX < 0); |
|
|
|
|
|
|
|
dismissRight = mVelocityTracker.getXVelocity() > 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (dismiss) { |
|
|
|
|
|
|
|
// dismiss
|
|
|
|
|
|
|
|
dismiss(mDownView, mDownPosition, dismissRight); |
|
|
|
} else { |
|
|
|
} else { |
|
|
|
mDownView = null; |
|
|
|
// cancel
|
|
|
|
|
|
|
|
mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); |
|
|
|
} |
|
|
|
} |
|
|
|
} |
|
|
|
mVelocityTracker.recycle(); |
|
|
|
view.onTouchEvent(motionEvent); |
|
|
|
mVelocityTracker = null; |
|
|
|
return true; |
|
|
|
mDownX = 0; |
|
|
|
} |
|
|
|
mDownView = null; |
|
|
|
|
|
|
|
mDownPosition = ListView.INVALID_POSITION; |
|
|
|
case MotionEvent.ACTION_UP: { |
|
|
|
mSwiping = false; |
|
|
|
if (mVelocityTracker == null) { |
|
|
|
|
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
float deltaX = motionEvent.getRawX() - mDownX; |
|
|
|
case MotionEvent.ACTION_CANCEL: { |
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
if (mVelocityTracker == null) { |
|
|
|
mVelocityTracker.computeCurrentVelocity(1000); |
|
|
|
break; |
|
|
|
float velocityX = mVelocityTracker.getXVelocity(); |
|
|
|
} |
|
|
|
float absVelocityX = Math.abs(velocityX); |
|
|
|
|
|
|
|
float absVelocityY = Math.abs(mVelocityTracker.getYVelocity()); |
|
|
|
|
|
|
|
boolean dismiss = false; |
|
|
|
|
|
|
|
boolean dismissRight = false; |
|
|
|
|
|
|
|
if (Math.abs(deltaX) > mViewWidth / 2) { |
|
|
|
|
|
|
|
dismiss = true; |
|
|
|
|
|
|
|
dismissRight = deltaX > 0; |
|
|
|
|
|
|
|
} else if (mMinFlingVelocity <= absVelocityX && absVelocityX <= mMaxFlingVelocity |
|
|
|
|
|
|
|
&& absVelocityY < absVelocityX) { |
|
|
|
|
|
|
|
// dismiss only if flinging in the same direction as dragging
|
|
|
|
|
|
|
|
dismiss = (velocityX < 0) == (deltaX < 0); |
|
|
|
|
|
|
|
dismissRight = mVelocityTracker.getXVelocity() > 0; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
if (dismiss) { |
|
|
|
|
|
|
|
// dismiss
|
|
|
|
|
|
|
|
dismiss(mDownView, mDownPosition, dismissRight); |
|
|
|
|
|
|
|
} else { |
|
|
|
|
|
|
|
// cancel
|
|
|
|
|
|
|
|
mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mVelocityTracker.recycle(); |
|
|
|
|
|
|
|
mVelocityTracker = null; |
|
|
|
|
|
|
|
mDownX = 0; |
|
|
|
|
|
|
|
mDownView = null; |
|
|
|
|
|
|
|
mDownPosition = ListView.INVALID_POSITION; |
|
|
|
|
|
|
|
mSwiping = false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_CANCEL: { |
|
|
|
if (mDownView != null) { |
|
|
|
if (mVelocityTracker == null) { |
|
|
|
// cancel
|
|
|
|
|
|
|
|
mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
mVelocityTracker.recycle(); |
|
|
|
|
|
|
|
mVelocityTracker = null; |
|
|
|
|
|
|
|
mDownX = 0; |
|
|
|
|
|
|
|
mDownView = null; |
|
|
|
|
|
|
|
mDownPosition = ListView.INVALID_POSITION; |
|
|
|
|
|
|
|
mSwiping = false; |
|
|
|
break; |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mDownView != null) { |
|
|
|
case MotionEvent.ACTION_MOVE: { |
|
|
|
// cancel
|
|
|
|
if (mVelocityTracker == null || mPaused) { |
|
|
|
mDownView.animate().translationX(0).alpha(1).setDuration(mAnimationTime).setListener(null); |
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
mVelocityTracker.recycle(); |
|
|
|
|
|
|
|
mVelocityTracker = null; |
|
|
|
|
|
|
|
mDownX = 0; |
|
|
|
|
|
|
|
mDownView = null; |
|
|
|
|
|
|
|
mDownPosition = ListView.INVALID_POSITION; |
|
|
|
|
|
|
|
mSwiping = false; |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
case MotionEvent.ACTION_MOVE: { |
|
|
|
|
|
|
|
if (mVelocityTracker == null || mPaused) { |
|
|
|
|
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
mVelocityTracker.addMovement(motionEvent); |
|
|
|
float deltaX = motionEvent.getRawX() - mDownX; |
|
|
|
float deltaX = motionEvent.getRawX() - mDownX; |
|
|
|
if (Math.abs(deltaX) > mSlop) { |
|
|
|
if (Math.abs(deltaX) > mSlop) { |
|
|
|
mSwiping = true; |
|
|
|
mSwiping = true; |
|
|
|
mListView.requestDisallowInterceptTouchEvent(true); |
|
|
|
mListView.requestDisallowInterceptTouchEvent(true); |
|
|
|
|
|
|
|
|
|
|
|
// Cancel ListView's touch (un-highlighting the item)
|
|
|
|
// Cancel ListView's touch (un-highlighting the item)
|
|
|
|
MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); |
|
|
|
MotionEvent cancelEvent = MotionEvent.obtain(motionEvent); |
|
|
|
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
|
|
|
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
|
|
|
| (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); |
|
|
|
| (motionEvent.getActionIndex() << MotionEvent.ACTION_POINTER_INDEX_SHIFT)); |
|
|
|
mListView.onTouchEvent(cancelEvent); |
|
|
|
mListView.onTouchEvent(cancelEvent); |
|
|
|
cancelEvent.recycle(); |
|
|
|
cancelEvent.recycle(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
if (mSwiping) { |
|
|
|
if (mSwiping) { |
|
|
|
mDownView.setTranslationX(deltaX); |
|
|
|
mDownView.setTranslationX(deltaX); |
|
|
|
mDownView.setAlpha(Math.max(0.15f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); |
|
|
|
mDownView.setAlpha(Math.max(0.15f, Math.min(1f, 1f - 2f * Math.abs(deltaX) / mViewWidth))); |
|
|
|
return true; |
|
|
|
return true; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
break; |
|
|
|
} |
|
|
|
} |
|
|
|
break; |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
} |
|
|
|
return false; |
|
|
|
return false; |
|
|
|
} |
|
|
|
} |
|
|
@ -324,7 +319,7 @@ public class SwipeDismissListViewTouchListener implements View.OnTouchListener { |
|
|
|
if (view == null) { |
|
|
|
if (view == null) { |
|
|
|
// No view, shortcut to calling onDismiss to let it deal with adapter
|
|
|
|
// No view, shortcut to calling onDismiss to let it deal with adapter
|
|
|
|
// updates and all that.
|
|
|
|
// updates and all that.
|
|
|
|
mCallbacks.onDismiss(mListView, new int[] { position }); |
|
|
|
mCallbacks.onDismiss(mListView, new int[]{position}); |
|
|
|
return; |
|
|
|
return; |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|