From 1efa3daf2d37ad6478ea3cad8d28708b5a03c770 Mon Sep 17 00:00:00 2001 From: Chaobin Wu Date: Wed, 20 Apr 2016 11:25:47 +0800 Subject: [PATCH] =?UTF-8?q?1.=E6=B6=88=E9=99=A4flag=E5=B8=B8=E9=87=8F?= =?UTF-8?q?=E8=AD=A6=E5=91=8A=EF=BC=9B2.=E8=B0=83=E6=95=B4offset=E6=AD=A7?= =?UTF-8?q?=E4=B9=89=EF=BC=9B3.header=20layout=E8=AE=A1=E7=AE=97top?= =?UTF-8?q?=E5=8F=AF=E8=AF=BB=E6=80=A7?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../srain/cube/views/ptr/PtrFrameLayout.java | 34 +++++++++++-------- 1 file changed, 20 insertions(+), 14 deletions(-) diff --git a/ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java b/ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java index 93e761c..dc1ea65 100755 --- a/ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java +++ b/ptr-lib/src/in/srain/cube/views/ptr/PtrFrameLayout.java @@ -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; @@ -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); @@ -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()); @@ -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(), @@ -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); }