|
| 1 | +package com.blankj.lib.base.slideBack; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.support.annotation.NonNull; |
| 5 | +import android.support.annotation.Nullable; |
| 6 | +import android.support.v4.view.MotionEventCompat; |
| 7 | +import android.util.AttributeSet; |
| 8 | +import android.view.MotionEvent; |
| 9 | +import android.view.VelocityTracker; |
| 10 | +import android.view.View; |
| 11 | +import android.view.ViewConfiguration; |
| 12 | +import android.widget.FrameLayout; |
| 13 | +import android.widget.Scroller; |
| 14 | + |
| 15 | +import com.blankj.utilcode.util.LogUtils; |
| 16 | + |
| 17 | +/** |
| 18 | + * <pre> |
| 19 | + * author: blankj |
| 20 | + * blog : http://blankj.com |
| 21 | + * time : 2019/03/20 |
| 22 | + * desc : |
| 23 | + * </pre> |
| 24 | + */ |
| 25 | +public class SlideBackLayout extends FrameLayout { |
| 26 | + |
| 27 | + private View mParentView; |
| 28 | + private int mEdgeSlop; |
| 29 | + private int mTouchSlop; |
| 30 | + |
| 31 | + private boolean isConsumeDown; |
| 32 | + |
| 33 | + private float mDownX; |
| 34 | + private float mDownY; |
| 35 | + private float mTempX; |
| 36 | + |
| 37 | + private Scroller mScroller; |
| 38 | + |
| 39 | + private boolean isFinish = false; |
| 40 | + |
| 41 | + private boolean isSliding = false; |
| 42 | + |
| 43 | + private int mViewWidth; |
| 44 | + |
| 45 | + private SlideListener mListener = new SlideListener() { |
| 46 | + @Override |
| 47 | + public void onStart() { |
| 48 | + LogUtils.e("start: "); |
| 49 | + } |
| 50 | + |
| 51 | + @Override |
| 52 | + public void onChange(float x) { |
| 53 | + LogUtils.e("onChange: " + x); |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public void onFinish() { |
| 58 | + LogUtils.e("onFinish: "); |
| 59 | + } |
| 60 | + }; |
| 61 | + |
| 62 | + private VelocityTracker mVelocityTracker; |
| 63 | + |
| 64 | + public SlideBackLayout(@NonNull Context context) { |
| 65 | + this(context, null); |
| 66 | + } |
| 67 | + |
| 68 | + public SlideBackLayout(@NonNull Context context, @Nullable AttributeSet attrs) { |
| 69 | + super(context, attrs); |
| 70 | + |
| 71 | + mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop(); |
| 72 | + mEdgeSlop = ViewConfiguration.get(context).getScaledEdgeSlop(); |
| 73 | + mScroller = new Scroller(context); |
| 74 | + } |
| 75 | + |
| 76 | + |
| 77 | + @Override |
| 78 | + protected void onLayout(boolean changed, int l, int t, int r, int b) { |
| 79 | + super.onLayout(changed, l, t, r, b); |
| 80 | + if (changed) { |
| 81 | + mParentView = (View) getParent(); |
| 82 | + mViewWidth = getWidth(); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + @Override |
| 87 | + public boolean dispatchTouchEvent(MotionEvent event) { |
| 88 | + final int action = event.getAction(); |
| 89 | + switch (action) { |
| 90 | + case MotionEvent.ACTION_DOWN: |
| 91 | + if (super.dispatchTouchEvent(event)) { |
| 92 | + isConsumeDown = true; |
| 93 | + } else { |
| 94 | + mDownX = mTempX = event.getRawX(); |
| 95 | + mDownY = event.getRawY(); |
| 96 | + } |
| 97 | + isSliding = false; |
| 98 | + return true; |
| 99 | + case MotionEvent.ACTION_MOVE: |
| 100 | + if (!isConsumeDown) { |
| 101 | + LogUtils.e(); |
| 102 | + if (isSliding) break; |
| 103 | + if (event.getRawX() - mDownX > mTouchSlop) { |
| 104 | + isSliding = true; |
| 105 | + if (mListener != null) { |
| 106 | + mListener.onStart(); |
| 107 | + } |
| 108 | + return true; |
| 109 | + } |
| 110 | + } |
| 111 | + break; |
| 112 | + default: |
| 113 | + break; |
| 114 | + } |
| 115 | + return super.dispatchTouchEvent(event); |
| 116 | + } |
| 117 | + |
| 118 | + @Override |
| 119 | + public boolean onTouchEvent(MotionEvent event) { |
| 120 | + LogUtils.e(event); |
| 121 | + if (isConsumeDown || !isSliding) return super.onTouchEvent(event); |
| 122 | + if (mVelocityTracker == null) { |
| 123 | + mVelocityTracker = VelocityTracker.obtain(); |
| 124 | + } |
| 125 | + mVelocityTracker.addMovement(event); |
| 126 | + final int action = MotionEventCompat.getActionMasked(event); |
| 127 | + switch (action) { |
| 128 | + case MotionEvent.ACTION_MOVE: |
| 129 | + int moveX = (int) event.getRawX(); |
| 130 | + if (moveX > mDownX) { |
| 131 | + mParentView.scrollBy((int) (mTempX - moveX), 0); |
| 132 | + } else if (moveX < mDownX) { |
| 133 | + //解决连续滑动Activity无法闭合的问题 |
| 134 | + mParentView.scrollTo(0, 0); |
| 135 | + } |
| 136 | + if (mListener != null) { |
| 137 | + mListener.onChange(mTempX - moveX); |
| 138 | + } |
| 139 | + mTempX = moveX; |
| 140 | + break; |
| 141 | + case MotionEvent.ACTION_UP: |
| 142 | + case MotionEvent.ACTION_CANCEL: |
| 143 | + startScroll(); |
| 144 | + break; |
| 145 | + } |
| 146 | + return true; |
| 147 | + } |
| 148 | + |
| 149 | + private void startScroll() { |
| 150 | + final VelocityTracker velocityTracker = mVelocityTracker; |
| 151 | + velocityTracker.computeCurrentVelocity(1000, ViewConfiguration.get(getContext()).getScaledMaximumFlingVelocity()); |
| 152 | + int xVelocity = (int) velocityTracker.getXVelocity(); |
| 153 | + LogUtils.e(xVelocity); |
| 154 | + |
| 155 | + if (mParentView.getScrollX() <= -mViewWidth / 2) { |
| 156 | + isFinish = true; |
| 157 | + scrollToRight(); |
| 158 | + } else { |
| 159 | + isFinish = false; |
| 160 | + scrollToOrigin(); |
| 161 | + } |
| 162 | + |
| 163 | + if (mListener != null) { |
| 164 | + mListener.onFinish(); |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void computeScroll() { |
| 170 | + if (mScroller.computeScrollOffset()) { |
| 171 | + mParentView.scrollTo(mScroller.getCurrX(), mScroller.getCurrY()); |
| 172 | + postInvalidate(); |
| 173 | + if (mScroller.isFinished() && isFinish && mListener != null) { |
| 174 | + mListener.onFinish(); |
| 175 | + } |
| 176 | + } |
| 177 | + } |
| 178 | + |
| 179 | + private void scrollToOrigin() { |
| 180 | + final int delta = mParentView.getScrollX(); |
| 181 | + mScroller.startScroll(mParentView.getScrollX(), 0, -delta, 0, Math.abs(delta)); |
| 182 | + postInvalidate(); |
| 183 | + } |
| 184 | + |
| 185 | + private void scrollToRight() { |
| 186 | + final int delta = mViewWidth + mParentView.getScrollX(); |
| 187 | + mScroller.startScroll(mParentView.getScrollX(), 0, -delta + 1, 0, Math.abs(delta)); |
| 188 | + postInvalidate(); |
| 189 | + } |
| 190 | + |
| 191 | + public interface SlideListener { |
| 192 | + void onStart(); |
| 193 | + |
| 194 | + void onChange(float x); |
| 195 | + |
| 196 | + void onFinish(); |
| 197 | + } |
| 198 | +} |
0 commit comments