From 825f8139a622ce2da8b1645c079620e5144c837e Mon Sep 17 00:00:00 2001 From: Rachilin Ivan Date: Mon, 18 Feb 2019 11:13:30 +0700 Subject: [PATCH] =?UTF-8?q?=D0=94=D0=B5=D0=BC=D0=BE=D0=BD=D1=81=D1=82?= =?UTF-8?q?=D1=80=D0=B0=D1=86=D0=B8=D1=8F=20=D1=81=D0=B2=D0=B0=D0=B9=D0=BF?= =?UTF-8?q?=D0=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../ItemTouchHelperExtension.java | 94 +++++++++++++++++-- 1 file changed, 86 insertions(+), 8 deletions(-) diff --git a/itemtouchhelperextension/src/main/java/com/loopeer/itemtouchhelperextension/ItemTouchHelperExtension.java b/itemtouchhelperextension/src/main/java/com/loopeer/itemtouchhelperextension/ItemTouchHelperExtension.java index ad3ef28..143bfd5 100644 --- a/itemtouchhelperextension/src/main/java/com/loopeer/itemtouchhelperextension/ItemTouchHelperExtension.java +++ b/itemtouchhelperextension/src/main/java/com/loopeer/itemtouchhelperextension/ItemTouchHelperExtension.java @@ -17,14 +17,7 @@ import android.support.v7.widget.RecyclerView.ViewHolder; import android.support.v7.widget.helper.ItemTouchUIUtil; import android.util.Log; -import android.view.GestureDetector; -import android.view.HapticFeedbackConstants; -import android.view.MotionEvent; -import android.view.VelocityTracker; -import android.view.View; -import android.view.ViewConfiguration; -import android.view.ViewGroup; -import android.view.ViewParent; +import android.view.*; import android.view.animation.AccelerateDecelerateInterpolator; import android.view.animation.Interpolator; @@ -207,6 +200,7 @@ public class ItemTouchHelperExtension extends RecyclerView.ItemDecoration private int mSlop; private RecyclerView mRecyclerView; + private ValueAnimator mDemoSwipe = null; /** * When user drags a view to the edge, we start scrolling the LayoutManager as long as View @@ -268,6 +262,11 @@ public void run() { @Override public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) { + if (mDemoSwipe != null) { + select(null, ACTION_STATE_SWIPE); + mDemoSwipe.cancel(); + mDemoSwipe = null; + } mGestureDetector.onTouchEvent(event); if (DEBUG) { Log.d(TAG, "intercept: x:" + event.getX() + ",y:" + event.getY() + ", " + event); @@ -321,6 +320,11 @@ public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent even @Override public void onTouchEvent(RecyclerView recyclerView, MotionEvent event) { + if (mDemoSwipe != null) { + select(null, ACTION_STATE_SWIPE); + mDemoSwipe.cancel(); + mDemoSwipe = null; + } mGestureDetector.onTouchEvent(event); if (DEBUG) { Log.d(TAG, @@ -1190,6 +1194,80 @@ public void startSwipe(ViewHolder viewHolder) { select(viewHolder, ACTION_STATE_SWIPE); } + /** + * Метод сдвигает элемент на dx + * + * @param viewHolder ViewHolder + * @param dx int + */ + public void demoSwipe(final ViewHolder viewHolder, int dx) { + startSwipe(viewHolder); + mDemoSwipe = ValueAnimator.ofInt(0, 40); + mDemoSwipe.setDuration(10000); + + mDemoSwipe.setInterpolator(new Interpolator() { + @Override + public float getInterpolation(float input) { + if (input < 1f / 16f) { + return (float) Math.sin(Math.PI * input * 16f); + } else if (input < 3f / 16f) { + return 0f; + } else if (input < 4f / 16f) { + return (float) Math.sin(Math.PI * (input - 3f / 16f) * 16f); + } else if (input < 6f / 16f) { + return 0f; + } else if (input < 7f / 16f) { + return (float) Math.sin(Math.PI * (input - 6f / 16f) * 16f); + } else if (input < 9f / 16f) { + return 0f; + } else if (input < 10f / 16f) { + return (float) Math.sin(Math.PI * (input - 9f / 16f) * 16f); + } else if (input < 12f / 16f) { + return 0f; + } else if (input < 13f / 16f) { + return (float) Math.sin(Math.PI * (input - 12f / 16f) * 16f); + } else if (input < 15f / 16f) { + return 0f; + } else { + return (float) Math.sin(Math.PI * (input - 15f / 16f) * 16f); + } + } + }); + + mDemoSwipe.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { + @Override + public void onAnimationUpdate(ValueAnimator animation) { + Log.d("DBG", "updateListener"); + mDx = Integer.valueOf(animation.getAnimatedValue().toString()); + moveIfNecessary(viewHolder); + mRecyclerView.removeCallbacks(mScrollRunnable); + mScrollRunnable.run(); + mRecyclerView.invalidate(); + viewHolder.itemView.invalidate(); + } + }); + mDemoSwipe.start(); + + mDemoSwipe.addListener(new Animator.AnimatorListener() { + @Override + public void onAnimationStart(Animator animation) { + } + + @Override + public void onAnimationEnd(Animator animation) { + mDemoSwipe = null; + } + + @Override + public void onAnimationCancel(Animator animation) { + } + + @Override + public void onAnimationRepeat(Animator animation) { + } + }); + } + private RecoverAnimation findAnimation(MotionEvent event) { if (mRecoverAnimations.isEmpty()) { return null;