|
14 | 14 | import android.view.ViewGroup;
|
15 | 15 | import android.widget.FrameLayout;
|
16 | 16 |
|
| 17 | +import java.util.ArrayList; |
| 18 | +import java.util.List; |
| 19 | + |
17 | 20 | public class SwipeBackLayout extends FrameLayout {
|
18 | 21 | /**
|
19 | 22 | * Minimum velocity that will be detected as a fling
|
@@ -93,7 +96,10 @@ public class SwipeBackLayout extends FrameLayout {
|
93 | 96 |
|
94 | 97 | private int mContentTop;
|
95 | 98 |
|
96 |
| - private SwipeListener mSwipeListener; |
| 99 | + /** |
| 100 | + * The set of listeners to be sent events through. |
| 101 | + */ |
| 102 | + private List<SwipeListener> mListeners; |
97 | 103 |
|
98 | 104 | private Drawable mShadowLeft;
|
99 | 105 |
|
@@ -210,9 +216,35 @@ public void setEdgeSize(int size) {
|
210 | 216 | * view.
|
211 | 217 | *
|
212 | 218 | * @param listener the swipe listener to attach to this view
|
| 219 | + * @deprecated use {@link #addSwipeListener} instead |
213 | 220 | */
|
| 221 | + @Deprecated |
214 | 222 | public void setSwipeListener(SwipeListener listener) {
|
215 |
| - mSwipeListener = listener; |
| 223 | + addSwipeListener(listener); |
| 224 | + } |
| 225 | + |
| 226 | + /** |
| 227 | + * Add a callback to be invoked when a swipe event is sent to this view. |
| 228 | + * |
| 229 | + * @param listener the swipe listener to attach to this view |
| 230 | + */ |
| 231 | + public void addSwipeListener(SwipeListener listener) { |
| 232 | + if (mListeners == null) { |
| 233 | + mListeners = new ArrayList<SwipeListener>(); |
| 234 | + } |
| 235 | + mListeners.add(listener); |
| 236 | + } |
| 237 | + |
| 238 | + /** |
| 239 | + * Removes a listener from the set of listeners |
| 240 | + * |
| 241 | + * @param listener |
| 242 | + */ |
| 243 | + public void removeSwipeListener(SwipeListener listener) { |
| 244 | + if (mListeners == null) { |
| 245 | + return; |
| 246 | + } |
| 247 | + mListeners.remove(listener); |
216 | 248 | }
|
217 | 249 |
|
218 | 250 | public static interface SwipeListener {
|
@@ -442,8 +474,10 @@ public boolean tryCaptureView(View view, int i) {
|
442 | 474 | } else if (mDragHelper.isEdgeTouched(EDGE_BOTTOM, i)) {
|
443 | 475 | mTrackingEdge = EDGE_BOTTOM;
|
444 | 476 | }
|
445 |
| - if (mSwipeListener != null) { |
446 |
| - mSwipeListener.onEdgeTouch(mTrackingEdge); |
| 477 | + if (mListeners != null && !mListeners.isEmpty()) { |
| 478 | + for (SwipeListener listener : mListeners) { |
| 479 | + listener.onEdgeTouch(mTrackingEdge); |
| 480 | + } |
447 | 481 | }
|
448 | 482 | mIsScrollOverValid = true;
|
449 | 483 | }
|
@@ -479,10 +513,13 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i
|
479 | 513 | if (mScrollPercent < mScrollThreshold && !mIsScrollOverValid) {
|
480 | 514 | mIsScrollOverValid = true;
|
481 | 515 | }
|
482 |
| - if (mSwipeListener != null && mDragHelper.getViewDragState() == STATE_DRAGGING |
| 516 | + if (mListeners != null && !mListeners.isEmpty() |
| 517 | + && mDragHelper.getViewDragState() == STATE_DRAGGING |
483 | 518 | && mScrollPercent >= mScrollThreshold && mIsScrollOverValid) {
|
484 | 519 | mIsScrollOverValid = false;
|
485 |
| - mSwipeListener.onScrollOverThreshold(); |
| 520 | + for (SwipeListener listener : mListeners) { |
| 521 | + listener.onScrollOverThreshold(); |
| 522 | + } |
486 | 523 | }
|
487 | 524 |
|
488 | 525 | if (mScrollPercent >= 1) {
|
@@ -535,8 +572,10 @@ public int clampViewPositionVertical(View child, int top, int dy) {
|
535 | 572 | @Override
|
536 | 573 | public void onViewDragStateChanged(int state) {
|
537 | 574 | super.onViewDragStateChanged(state);
|
538 |
| - if (mSwipeListener != null) { |
539 |
| - mSwipeListener.onScrollStateChange(state, mScrollPercent); |
| 575 | + if (mListeners != null && !mListeners.isEmpty()) { |
| 576 | + for (SwipeListener listener : mListeners) { |
| 577 | + listener.onScrollStateChange(state, mScrollPercent); |
| 578 | + } |
540 | 579 | }
|
541 | 580 | }
|
542 | 581 | }
|
|
0 commit comments