Skip to content

Commit

Permalink
Merge pull request #222 from avenwu/master
Browse files Browse the repository at this point in the history
Polish the code readability
  • Loading branch information
uknownothingsnow committed May 4, 2016
2 parents f5f838f + 1efa3da commit 46fdc86
Showing 1 changed file with 20 additions and 14 deletions.
34 changes: 20 additions & 14 deletions ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,11 @@ public class PtrFrameLayout extends ViewGroup {
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;
private final static byte FLAG_AUTO_REFRESH_AT_ONCE = 0x01;
private final static byte FLAG_AUTO_REFRESH_BUT_LATER = 0x01 << 1;
private final static byte FLAG_ENABLE_NEXT_PTR_AT_ONCE = 0x01 << 2;
private final static byte FLAG_PIN_CONTENT = 0x01 << 3;
private final static byte MASK_AUTO_REFRESH = 0x03;
protected View mContent;
// optional config for define header and content in xml file
private int mHeaderId = 0;
Expand Down Expand Up @@ -114,7 +114,7 @@ public PtrFrameLayout(Context context, AttributeSet attrs, int defStyle) {
protected void onFinishInflate() {
final int childCount = getChildCount();
if (childCount > 2) {
throw new IllegalStateException("PtrFrameLayout only can host 2 elements");
throw new IllegalStateException("PtrFrameLayout can only contains 2 children");
} else if (childCount == 2) {
if (mHeaderId != 0 && mHeaderView == null) {
mHeaderView = findViewById(mHeaderId);
Expand Down Expand Up @@ -184,7 +184,7 @@ protected void onDetachedFromWindow() {
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);

if (DEBUG && DEBUG_LAYOUT) {
if (isDebug()) {
PtrCLog.d(LOG_TAG, "onMeasure frame: width: %s, height: %s, padding: %s %s %s %s",
getMeasuredHeight(), getMeasuredWidth(),
getPaddingLeft(), getPaddingRight(), getPaddingTop(), getPaddingBottom());
Expand All @@ -200,7 +200,7 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

if (mContent != null) {
measureContentView(mContent, widthMeasureSpec, heightMeasureSpec);
if (DEBUG && DEBUG_LAYOUT) {
if (isDebug()) {
ViewGroup.MarginLayoutParams lp = (MarginLayoutParams) mContent.getLayoutParams();
PtrCLog.d(LOG_TAG, "onMeasure content, width: %s, height: %s, margin: %s %s %s %s",
getMeasuredWidth(), getMeasuredHeight(),
Expand Down Expand Up @@ -230,37 +230,43 @@ protected void onLayout(boolean flag, int i, int j, int k, int l) {
}

private void layoutChildren() {
int offsetX = mPtrIndicator.getCurrentPosY();
int offset = mPtrIndicator.getCurrentPosY();
int paddingLeft = getPaddingLeft();
int paddingTop = getPaddingTop();

if (mHeaderView != null) {
MarginLayoutParams lp = (MarginLayoutParams) mHeaderView.getLayoutParams();
final int left = paddingLeft + lp.leftMargin;
final int top = paddingTop + lp.topMargin + offsetX - mHeaderHeight;
// enhance readability(header is layout above screen when first init)
final int top = -(mHeaderHeight - paddingTop - lp.topMargin - offset);
final int right = left + mHeaderView.getMeasuredWidth();
final int bottom = top + mHeaderView.getMeasuredHeight();
mHeaderView.layout(left, top, right, bottom);
if (DEBUG && DEBUG_LAYOUT) {
if (isDebug()) {
PtrCLog.d(LOG_TAG, "onLayout header: %s %s %s %s", left, top, right, bottom);
}
}
if (mContent != null) {
if (isPinContent()) {
offsetX = 0;
offset = 0;
}
MarginLayoutParams lp = (MarginLayoutParams) mContent.getLayoutParams();
final int left = paddingLeft + lp.leftMargin;
final int top = paddingTop + lp.topMargin + offsetX;
final int top = paddingTop + lp.topMargin + offset;
final int right = left + mContent.getMeasuredWidth();
final int bottom = top + mContent.getMeasuredHeight();
if (DEBUG && DEBUG_LAYOUT) {
if (isDebug()) {
PtrCLog.d(LOG_TAG, "onLayout content: %s %s %s %s", left, top, right, bottom);
}
mContent.layout(left, top, right, bottom);
}
}

@SuppressWarnings({"PointlessBooleanExpression", "ConstantConditions"})
private boolean isDebug() {
return DEBUG && DEBUG_LAYOUT;
}

public boolean dispatchTouchEventSupper(MotionEvent e) {
return super.dispatchTouchEvent(e);
}
Expand Down

0 comments on commit 46fdc86

Please sign in to comment.