1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255
|
public class SwipeListLayout extends FrameLayout {
private View hiddenView; private View itemView; private int hiddenViewWidth; private ViewDragHelper mDragHelper; private int hiddenViewHeight; private int itemWidth; private int itemHeight; private OnSwipeStatusListener listener; private Status status = Status.Close; private boolean smooth = true;
public static final String TAG = "SlipListLayout";
public enum Status { Open, Close }
public void setStatus(Status status, boolean smooth) { this.status = status; if (status == Status.Open) { open(smooth); } else { close(smooth); } }
public void setOnSwipeStatusListener(OnSwipeStatusListener listener) { this.listener = listener; }
public void setSmooth(boolean smooth) { this.smooth = smooth; }
public interface OnSwipeStatusListener {
void onStatusChanged(Status status);
void onStartCloseAnimation();
void onStartOpenAnimation();
}
public SwipeListLayout(Context context) { this(context, null); }
public SwipeListLayout(Context context, AttributeSet attrs) { super(context, attrs); mDragHelper = ViewDragHelper.create(this, callback); }
Callback callback = new Callback() {
@Override public boolean tryCaptureView(View view, int arg1) { return view == itemView; }
@Override public int clampViewPositionHorizontal(View child, int left, int dx) { if (child == itemView) { if (left > 0) { return 0; } else { left = Math.max(left, -hiddenViewWidth); return left; } } return 0; }
@Override public int getViewHorizontalDragRange(View child) { return hiddenViewWidth; }
@Override public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) { if (itemView == changedView) { hiddenView.offsetLeftAndRight(dx); } invalidate(); }
public void onViewReleased(View releasedChild, float xvel, float yvel) { if (releasedChild == itemView) { if (xvel == 0 && Math.abs(itemView.getLeft()) > hiddenViewWidth / 2.0f) { open(smooth); } else if (xvel < 0) { open(smooth); } else { close(smooth); } } }
}; private Status preStatus = Status.Close;
private void close(boolean smooth) { preStatus = status; status = Status.Close; if (smooth) { if (mDragHelper.smoothSlideViewTo(itemView, 0, 0)) { if (listener != null) { Log.i(TAG, "start close animation"); listener.onStartCloseAnimation(); } ViewCompat.postInvalidateOnAnimation(this); } } else { layout(status); } if (listener != null && preStatus == Status.Open) { Log.i(TAG, "close"); listener.onStatusChanged(status); } }
private void layout(Status status) { if (status == Status.Close) { hiddenView.layout(itemWidth, 0, itemWidth + hiddenViewWidth, itemHeight); itemView.layout(0, 0, itemWidth, itemHeight); } else { hiddenView.layout(itemWidth - hiddenViewWidth, 0, itemWidth, itemHeight); itemView.layout(-hiddenViewWidth, 0, itemWidth - hiddenViewWidth, itemHeight); } }
private void open(boolean smooth) { preStatus = status; status = Status.Open; if (smooth) { if (mDragHelper.smoothSlideViewTo(itemView, -hiddenViewWidth, 0)) { if (listener != null) { Log.i(TAG, "start open animation"); listener.onStartOpenAnimation(); } ViewCompat.postInvalidateOnAnimation(this); } } else { layout(status); } if (listener != null && preStatus == Status.Close) { Log.i(TAG, "open"); listener.onStatusChanged(status); } }
@Override public void computeScroll() { super.computeScroll(); if (mDragHelper.continueSettling(true)) { ViewCompat.postInvalidateOnAnimation(this); } }
public boolean onInterceptTouchEvent(MotionEvent ev) { final int action = ev.getAction(); if (action == MotionEvent.ACTION_CANCEL) { mDragHelper.cancel(); return false; } return mDragHelper.shouldInterceptTouchEvent(ev); }
public boolean onTouchEvent(MotionEvent event) { mDragHelper.processTouchEvent(event); return true; };
@Override protected void onFinishInflate() { super.onFinishInflate(); hiddenView = getChildAt(0); itemView = getChildAt(1); }
@Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); itemWidth = itemView.getMeasuredWidth(); itemHeight = itemView.getMeasuredHeight(); hiddenViewWidth = hiddenView.getMeasuredWidth(); hiddenViewHeight = hiddenView.getMeasuredHeight(); }
@Override protected void onLayout(boolean changed, int left, int top, int right, int bottom) { layout(Status.Close); }
}
|