Skip to content

Commit

Permalink
fix memory leak, fix #88, fix #103, fix #130
Browse files Browse the repository at this point in the history
  • Loading branch information
liaohuqiu committed Sep 19, 2015
1 parent 25d2c0d commit ab9b4fc
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 23 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,14 @@ protected void initViews(AttributeSet attrs) {
resetView();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mLastUpdateTimeUpdater != null) {
mLastUpdateTimeUpdater.stop();
}
}

public void setRotateAniTime(int time) {
if (time == mRotateAniTime || time == 0) {
return;
Expand Down
61 changes: 38 additions & 23 deletions ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,23 +18,20 @@ public class PtrFrameLayout extends ViewGroup {

// status enum
public final static byte PTR_STATUS_INIT = 1;
private byte mStatus = PTR_STATUS_INIT;
public final static byte PTR_STATUS_PREPARE = 2;
public final static byte PTR_STATUS_LOADING = 3;
public final static byte PTR_STATUS_COMPLETE = 4;

private static final boolean DEBUG_LAYOUT = true;
public static boolean DEBUG = false;
private static int ID = 1;

protected final String LOG_TAG = "ptr-frame-" + ++ID;
// auto refresh status
private static byte FLAG_AUTO_REFRESH_AT_ONCE = 0x01;
private static byte FLAG_AUTO_REFRESH_BUT_LATER = 0x01 << 1;
private static byte FLAG_ENABLE_NEXT_PTR_AT_ONCE = 0x01 << 2;
private static byte FLAG_PIN_CONTENT = 0x01 << 3;

private static byte MASK_AUTO_REFRESH = 0x03;

protected final String LOG_TAG = "ptr-frame-" + ++ID;
protected View mContent;
// optional config for define header and content in xml file
private int mHeaderId = 0;
Expand All @@ -51,8 +48,6 @@ public class PtrFrameLayout extends ViewGroup {
private ScrollChecker mScrollChecker;
private int mPagingTouchSlop;
private int mHeaderHeight;

private byte mStatus = PTR_STATUS_INIT;
private boolean mDisableWhenHorizontalMove = false;
private int mFlag = 0x00;

Expand All @@ -67,6 +62,12 @@ public class PtrFrameLayout extends ViewGroup {
private long mLoadingStartTime = 0;
private PtrIndicator mPtrIndicator;
private boolean mHasSendCancelEvent = false;
private Runnable mPerformRefreshCompleteDelay = new Runnable() {
@Override
public void run() {
performRefreshComplete();
}
};

public PtrFrameLayout(Context context) {
this(context, null);
Expand Down Expand Up @@ -167,6 +168,18 @@ protected void onFinishInflate() {
super.onFinishInflate();
}

@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mScrollChecker != null) {
mScrollChecker.destroy();
}

if (mPerformRefreshCompleteDelay != null) {
removeCallbacks(mPerformRefreshCompleteDelay);
}
}

@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
Expand Down Expand Up @@ -592,12 +605,7 @@ final public void refreshComplete() {
}
performRefreshComplete();
} else {
postDelayed(new Runnable() {
@Override
public void run() {
performRefreshComplete();
}
}, delay);
postDelayed(mPerformRefreshCompleteDelay, delay);
if (DEBUG) {
PtrCLog.d(LOG_TAG, "performRefreshComplete after delay: %s", delay);
}
Expand Down Expand Up @@ -694,6 +702,10 @@ private boolean performAutoRefreshButLater() {
return (mFlag & MASK_AUTO_REFRESH) == FLAG_AUTO_REFRESH_BUT_LATER;
}

public boolean isEnabledNextPtrAtOnce() {
return (mFlag & FLAG_ENABLE_NEXT_PTR_AT_ONCE) > 0;
}

/**
* If @param enable has been set to true. The user can perform next PTR at once.
*
Expand All @@ -707,8 +719,8 @@ public void setEnabledNextPtrAtOnce(boolean enable) {
}
}

public boolean isEnabledNextPtrAtOnce() {
return (mFlag & FLAG_ENABLE_NEXT_PTR_AT_ONCE) > 0;
public boolean isPinContent() {
return (mFlag & FLAG_PIN_CONTENT) > 0;
}

/**
Expand All @@ -724,10 +736,6 @@ public void setPinContent(boolean pinContent) {
}
}

public boolean isPinContent() {
return (mFlag & FLAG_PIN_CONTENT) > 0;
}

/**
* It's useful when working with viewpager.
*
Expand Down Expand Up @@ -836,13 +844,13 @@ public float getRatioOfHeaderToHeightRefresh() {
}

@SuppressWarnings({"unused"})
public void setOffsetToKeepHeaderWhileLoading(int offset) {
mPtrIndicator.setOffsetToKeepHeaderWhileLoading(offset);
public int getOffsetToKeepHeaderWhileLoading() {
return mPtrIndicator.getOffsetToKeepHeaderWhileLoading();
}

@SuppressWarnings({"unused"})
public int getOffsetToKeepHeaderWhileLoading() {
return mPtrIndicator.getOffsetToKeepHeaderWhileLoading();
public void setOffsetToKeepHeaderWhileLoading(int offset) {
mPtrIndicator.setOffsetToKeepHeaderWhileLoading(offset);
}

@SuppressWarnings({"unused"})
Expand Down Expand Up @@ -989,6 +997,13 @@ private void reset() {
removeCallbacks(this);
}

private void destroy() {
reset();
if (!mScroller.isFinished()) {
mScroller.forceFinished(true);
}
}

public void abortIfWorking() {
if (mIsRunning) {
if (!mScroller.isFinished()) {
Expand Down

0 comments on commit ab9b4fc

Please sign in to comment.