Skip to content

Commit 9560c70

Browse files
author
Issac
committed
重构代码,在4.4中将回收多余的Surface,已提升性能
1 parent 845f49f commit 9560c70

File tree

3 files changed

+67
-15
lines changed

3 files changed

+67
-15
lines changed

library/src/main/java/me/imid/swipebacklayout/lib/SwipeBackLayout.java

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,9 @@
1414
import android.view.ViewGroup;
1515
import android.widget.FrameLayout;
1616

17+
import java.util.ArrayList;
18+
import java.util.List;
19+
1720
public class SwipeBackLayout extends FrameLayout {
1821
/**
1922
* Minimum velocity that will be detected as a fling
@@ -93,7 +96,10 @@ public class SwipeBackLayout extends FrameLayout {
9396

9497
private int mContentTop;
9598

96-
private SwipeListener mSwipeListener;
99+
/**
100+
* The set of listeners to be sent events through.
101+
*/
102+
private List<SwipeListener> mListeners;
97103

98104
private Drawable mShadowLeft;
99105

@@ -210,9 +216,35 @@ public void setEdgeSize(int size) {
210216
* view.
211217
*
212218
* @param listener the swipe listener to attach to this view
219+
* @deprecated use {@link #addSwipeListener} instead
213220
*/
221+
@Deprecated
214222
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);
216248
}
217249

218250
public static interface SwipeListener {
@@ -442,8 +474,10 @@ public boolean tryCaptureView(View view, int i) {
442474
} else if (mDragHelper.isEdgeTouched(EDGE_BOTTOM, i)) {
443475
mTrackingEdge = EDGE_BOTTOM;
444476
}
445-
if (mSwipeListener != null) {
446-
mSwipeListener.onEdgeTouch(mTrackingEdge);
477+
if (mListeners != null && !mListeners.isEmpty()) {
478+
for (SwipeListener listener : mListeners) {
479+
listener.onEdgeTouch(mTrackingEdge);
480+
}
447481
}
448482
mIsScrollOverValid = true;
449483
}
@@ -479,10 +513,13 @@ public void onViewPositionChanged(View changedView, int left, int top, int dx, i
479513
if (mScrollPercent < mScrollThreshold && !mIsScrollOverValid) {
480514
mIsScrollOverValid = true;
481515
}
482-
if (mSwipeListener != null && mDragHelper.getViewDragState() == STATE_DRAGGING
516+
if (mListeners != null && !mListeners.isEmpty()
517+
&& mDragHelper.getViewDragState() == STATE_DRAGGING
483518
&& mScrollPercent >= mScrollThreshold && mIsScrollOverValid) {
484519
mIsScrollOverValid = false;
485-
mSwipeListener.onScrollOverThreshold();
520+
for (SwipeListener listener : mListeners) {
521+
listener.onScrollOverThreshold();
522+
}
486523
}
487524

488525
if (mScrollPercent >= 1) {
@@ -535,8 +572,10 @@ public int clampViewPositionVertical(View child, int top, int dy) {
535572
@Override
536573
public void onViewDragStateChanged(int state) {
537574
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+
}
540579
}
541580
}
542581
}

library/src/main/java/me/imid/swipebacklayout/lib/app/SwipeBackActivityHelper.java

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33

44
import android.app.Activity;
55
import android.graphics.drawable.ColorDrawable;
6-
import android.util.Log;
76
import android.view.LayoutInflater;
87
import android.view.View;
98

@@ -29,11 +28,28 @@ public void onActivityCreate() {
2928
mActivity.getWindow().getDecorView().setBackgroundDrawable(null);
3029
mSwipeBackLayout = (SwipeBackLayout) LayoutInflater.from(mActivity).inflate(
3130
me.imid.swipebacklayout.lib.R.layout.swipeback_layout, null);
31+
mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
32+
@Override
33+
public void onScrollStateChange(int state, float scrollPercent) {
34+
if (state == SwipeBackLayout.STATE_IDLE && scrollPercent == 0) {
35+
convertActivityFromTranslucent();
36+
}
37+
}
38+
39+
@Override
40+
public void onEdgeTouch(int edgeFlag) {
41+
convertActivityToTranslucent();
42+
}
43+
44+
@Override
45+
public void onScrollOverThreshold() {
46+
47+
}
48+
});
3249
}
3350

3451
public void onPostCreate() {
3552
mSwipeBackLayout.attachToActivity(mActivity);
36-
convertActivityFromTranslucent();
3753
}
3854

3955
public View findViewById(int id) {
@@ -85,11 +101,8 @@ public void convertActivityToTranslucent() {
85101
Class<?>[] classes = Activity.class.getDeclaredClasses();
86102
Class<?> translucentConversionListenerClazz = null;
87103
for (Class clazz : classes) {
88-
if (clazz.getName().contains("TranslucentConversionListener")) {
104+
if (clazz.getSimpleName().contains("TranslucentConversionListener")) {
89105
translucentConversionListenerClazz = clazz;
90-
Log.e("class ",
91-
clazz.getName() + "," + clazz.getCanonicalName() + ","
92-
+ clazz.getSimpleName());
93106
}
94107
}
95108
Method method = Activity.class.getDeclaredMethod("convertToTranslucent",

samples/src/main/java/me/imid/swipebacklayout/demo/DemoActivity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ public void onCheckedChanged(RadioGroup group, int checkedId) {
6262
saveTrackingMode(edgeFlag);
6363
}
6464
});
65-
mSwipeBackLayout.setSwipeListener(new SwipeBackLayout.SwipeListener() {
65+
mSwipeBackLayout.addSwipeListener(new SwipeBackLayout.SwipeListener() {
6666
@Override
6767
public void onScrollStateChange(int state, float scrollPercent) {
6868

0 commit comments

Comments
 (0)