From a8b6582c0efcc43bfa2046ca8ee637afc16e73e1 Mon Sep 17 00:00:00 2001 From: song <764469385@qq.com> Date: Fri, 21 Jul 2017 11:47:50 +0800 Subject: [PATCH] =?UTF-8?q?-=E6=94=AF=E6=8C=81xml=E6=B7=BB=E5=8A=A0head=20?= =?UTF-8?q?foot?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../song/refreshlayout/QSRefreshLayout.java | 39 +++++++++++++++++-- .../refreshview/AnimeRefreshView.java | 9 ++++- .../refreshview/BarRefreshView.java | 9 ++++- .../refreshview/CircleImageView.java | 21 +++++++--- .../refreshview/XMLRefreshView.java | 9 ++++- RefreshLayout/src/main/res/layout/refresh.xml | 1 + RefreshLayout/src/main/res/values/ids.xml | 5 +++ .../refreshview/ElemeRefreshView.java | 9 +++-- .../refreshview/JDRefreshView.java | 10 +++-- .../refreshview/PlainRefreshDraw.java | 6 +++ .../refreshview/PlainRefreshView.java | 18 +++++++-- .../refreshview/SunRefreshDraw.java | 8 ++++ .../refreshview/SunRefreshView.java | 17 ++++++-- app/src/main/res/layout/activity_recyle.xml | 1 + 14 files changed, 136 insertions(+), 26 deletions(-) create mode 100644 RefreshLayout/src/main/res/values/ids.xml diff --git a/RefreshLayout/src/main/java/org/song/refreshlayout/QSRefreshLayout.java b/RefreshLayout/src/main/java/org/song/refreshlayout/QSRefreshLayout.java index 49490ff..b2ddf31 100644 --- a/RefreshLayout/src/main/java/org/song/refreshlayout/QSRefreshLayout.java +++ b/RefreshLayout/src/main/java/org/song/refreshlayout/QSRefreshLayout.java @@ -26,12 +26,20 @@ public QSRefreshLayout(Context context) { public QSRefreshLayout(Context context, AttributeSet attrs) { super(context, attrs); - initData(); + post(new Runnable() { + @Override + public void run() { + initData(); + + } + }); } private void initData() { - setHeadRefreshView(new CircleImageView(getContext())); - setFootRefreshView(new BarRefreshView(getContext())); + if (!ensureRefreshView()) { + setHeadRefreshView(new CircleImageView(getContext())); + setFootRefreshView(new BarRefreshView(getContext())); + } } public void setHeadRefreshView(IRefreshView headRefreshView) { @@ -88,6 +96,31 @@ protected View ensureTarget() { return mTarget; } + protected boolean ensureRefreshView() { + boolean b = false; + if (getChildCount() > 0) { + for (int i = 0; i < getChildCount(); i++) { + View child = getChildAt(i); + if (child instanceof IRefreshView) { + IRefreshView v = (IRefreshView) child; + if (!b) { + headRefreshView = v; + headRefreshView.isHeadView(true); + headRefreshView.getView().setVisibility(INVISIBLE); + isOpenHeadRefresh(true); + } else { + footRefreshView = v; + footRefreshView.isHeadView(false); + footRefreshView.getView().setVisibility(INVISIBLE); + isOpenFootRefresh(true); + } + b = true; + } + } + } + return b; + } + @Override protected boolean canChildScrollUp() { if (android.os.Build.VERSION.SDK_INT < 14) { diff --git a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/AnimeRefreshView.java b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/AnimeRefreshView.java index b756b38..542536a 100644 --- a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/AnimeRefreshView.java +++ b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/AnimeRefreshView.java @@ -2,7 +2,9 @@ import android.content.Context; import android.graphics.drawable.AnimationDrawable; +import android.util.AttributeSet; import android.view.View; +import android.view.ViewGroup; import android.widget.ImageView; import org.song.refreshlayout.IRefreshView; @@ -20,8 +22,13 @@ public class AnimeRefreshView extends ImageView implements IRefreshView { private int tempIndex; private AnimationDrawable animationDrawable; + public AnimeRefreshView(Context context) { - super(context); + this(context, null); + } + + public AnimeRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); } public void setDrawAnimes(int[] animes) { diff --git a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/BarRefreshView.java b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/BarRefreshView.java index 966958e..e1c7652 100644 --- a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/BarRefreshView.java +++ b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/BarRefreshView.java @@ -2,6 +2,7 @@ import android.content.Context; import android.graphics.Canvas; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; @@ -20,7 +21,11 @@ public class BarRefreshView extends View implements IRefreshView { private SwipeProgressBar swipeProgressBar; public BarRefreshView(Context context) { - super(context); + this(context, null); + } + + public BarRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); float density = context.getResources().getDisplayMetrics().density; swipeProgressBar = new SwipeProgressBar(this); @@ -37,7 +42,7 @@ public void setColorScheme(int... color1) { @Override//确定view大小 protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); - swipeProgressBar.setBounds(0, 0, getMeasuredWidth(), getMeasuredHeight()); + swipeProgressBar.setBounds(0, 0, getMeasuredWidth(), height = getMeasuredHeight()); } diff --git a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/CircleImageView.java b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/CircleImageView.java index b84466c..7062de0 100644 --- a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/CircleImageView.java +++ b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/CircleImageView.java @@ -9,6 +9,7 @@ import android.graphics.drawable.ShapeDrawable; import android.graphics.drawable.shapes.OvalShape; import android.support.v4.view.ViewCompat; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.view.animation.Animation; @@ -40,7 +41,11 @@ public class CircleImageView extends ImageView implements IRefreshView { @SuppressWarnings("deprecation") public CircleImageView(Context context) { - super(context); + this(context, null); + } + + public CircleImageView(Context context, AttributeSet attrs) { + super(context, attrs); init(); } @@ -78,15 +83,17 @@ private void init() { setLayoutParams(new ViewGroup.LayoutParams(diameter, diameter)); } + @Override//锁死view大小 + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + setMeasuredDimension(diameter, diameter); + } + + //设置大小 public void setRadius(int radius) { this.radius = radius; init(); } - private boolean elevationSupported() { - return android.os.Build.VERSION.SDK_INT >= 21; - } - public void setColorScheme(int... color1) { materialProgressDrawable.setColorSchemeColors(color1); } @@ -197,6 +204,10 @@ public void isHeadView(boolean isHead) { } + private boolean elevationSupported() { + return android.os.Build.VERSION.SDK_INT >= 21; + } + private class OvalShadow extends OvalShape { private final RadialGradient mRadialGradient; diff --git a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/XMLRefreshView.java b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/XMLRefreshView.java index cd406a1..d7774c8 100644 --- a/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/XMLRefreshView.java +++ b/RefreshLayout/src/main/java/org/song/refreshlayout/refreshview/XMLRefreshView.java @@ -3,6 +3,7 @@ import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.text.TextUtils; +import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; @@ -34,7 +35,11 @@ public class XMLRefreshView extends FrameLayout implements IRefreshView { refreshed = "刷新完成"; public XMLRefreshView(Context context) { - super(context); + this(context, null); + } + + public XMLRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); refreshView = (ViewGroup) LayoutInflater.from(context).inflate(R.layout.refresh, this, false); refreshTV = (TextView) refreshView.findViewById(R.id.refresh_tv); refreshIV = (ImageView) refreshView.findViewById(R.id.refresh_iv); @@ -136,7 +141,7 @@ public void isHeadView(boolean isHead) { dragging = isHead ? "下拉刷新" : "上拉刷新"; } - public void setBlackStyle(){ + public void setBlackStyle() { } diff --git a/RefreshLayout/src/main/res/layout/refresh.xml b/RefreshLayout/src/main/res/layout/refresh.xml index 12f22e3..aab5d27 100644 --- a/RefreshLayout/src/main/res/layout/refresh.xml +++ b/RefreshLayout/src/main/res/layout/refresh.xml @@ -3,6 +3,7 @@ android:layout_width="match_parent" android:layout_height="60dp" android:gravity="center" + android:layout_gravity="center" android:orientation="horizontal"> + + + + \ No newline at end of file diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/ElemeRefreshView.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/ElemeRefreshView.java index e8db9a7..2b5850f 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/ElemeRefreshView.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/ElemeRefreshView.java @@ -5,6 +5,7 @@ import android.animation.ObjectAnimator; import android.content.Context; import android.os.Handler; +import android.util.AttributeSet; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; @@ -34,7 +35,11 @@ public class ElemeRefreshView extends FrameLayout implements IRefreshView { float density; public ElemeRefreshView(Context context) { - super(context); + this(context, null); + } + + public ElemeRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); density = context.getResources().getDisplayMetrics().density; triggerDistance = (int) (density * 100); @@ -45,8 +50,6 @@ public ElemeRefreshView(Context context) { eleme_content_box = (ViewGroup) refreshView.findViewById(R.id.eleme_content_box); addView(refreshView); - - } @Override diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/JDRefreshView.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/JDRefreshView.java index c227a42..75d1965 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/JDRefreshView.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/JDRefreshView.java @@ -2,7 +2,7 @@ import android.content.Context; import android.graphics.drawable.AnimationDrawable; -import android.support.annotation.NonNull; +import android.util.AttributeSet; import android.view.LayoutInflater; import android.view.View; import android.widget.FrameLayout; @@ -25,8 +25,12 @@ public class JDRefreshView extends FrameLayout implements IRefreshView { TextView jd_text; int status; - public JDRefreshView(@NonNull Context context) { - super(context); + public JDRefreshView(Context context) { + this(context, null); + } + + public JDRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); View refreshView = LayoutInflater.from(context).inflate(R.layout.refresh_jd, this, false); jd_text = (TextView) refreshView.findViewById(R.id.jd_text); jd_people = (ImageView) refreshView.findViewById(R.id.jd_people); diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshDraw.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshDraw.java index 99714ba..76bf6ed 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshDraw.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshDraw.java @@ -91,6 +91,8 @@ public class PlainRefreshDraw extends Drawable implements Drawable.Callback, Ani private Random mRandom; private boolean mEndOfRefreshing; + + private int getTotalDragDistance; private enum AnimationPart { @@ -122,6 +124,10 @@ private void initiateDimens() { mScreenWidth = mContext.getResources().getDisplayMetrics().widthPixels; mJetTopOffset = getTotalDragDistance * 0.5f; } + public void setGetTotalDragDistance(int getTotalDragDistance) { + this.getTotalDragDistance = getTotalDragDistance; + mJetTopOffset = getTotalDragDistance * 0.5f; + } private void createBitmaps() { mLeftClouds = BitmapFactory.decodeResource(mContext.getResources(), R.drawable.clouds_left); diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshView.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshView.java index 47c91e8..a5f8b15 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshView.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/PlainRefreshView.java @@ -1,6 +1,7 @@ package com.song.qsrefreshlayout.refreshview; import android.content.Context; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -18,15 +19,24 @@ public class PlainRefreshView extends ImageView implements IRefreshView { PlainRefreshDraw plainRefreshDraw; public PlainRefreshView(Context context) { - super(context); + this(context, null); + } + + public PlainRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); float density = context.getResources().getDisplayMetrics().density; - mTotalDragDistance = Math.round((float) 120 * density); - setLayoutParams(new ViewGroup.LayoutParams(-1, mTotalDragDistance)); + setLayoutParams(new ViewGroup.LayoutParams(-1, mTotalDragDistance = Math.round((float) 120 * density))); - plainRefreshDraw = new PlainRefreshDraw(context, mTotalDragDistance); + plainRefreshDraw = new PlainRefreshDraw(getContext(), mTotalDragDistance); setImageDrawable(plainRefreshDraw); } + @Override//确定view大小 + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + plainRefreshDraw.setGetTotalDragDistance(mTotalDragDistance = getMeasuredHeight()); + } + @Override public View getView() { return this; diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshDraw.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshDraw.java index 7c74372..aa4fe48 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshDraw.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshDraw.java @@ -38,6 +38,7 @@ public class SunRefreshDraw extends SunBaseRefreshDraw implements Animatable { private static final Interpolator LINEAR_INTERPOLATOR = new LinearInterpolator(); + private int getTotalDragDistance; private Matrix mMatrix; @@ -93,6 +94,13 @@ public void initiateDimens(int viewWidth) { createBitmaps(); } + public void setGetTotalDragDistance(int getTotalDragDistance) { + this.getTotalDragDistance = getTotalDragDistance; + mTownInitialTopOffset = (getTotalDragDistance - mTownHeight * TOWN_INITIAL_SCALE); + mTownFinalTopOffset = (getTotalDragDistance - mTownHeight * TOWN_FINAL_SCALE); + mSunTopOffset = (getTotalDragDistance * 0.1f); + } + private void createBitmaps() { final BitmapFactory.Options options = new BitmapFactory.Options(); options.inPreferredConfig = Bitmap.Config.RGB_565; diff --git a/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshView.java b/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshView.java index 88b987c..d8881a2 100644 --- a/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshView.java +++ b/app/src/main/java/com/song/qsrefreshlayout/refreshview/SunRefreshView.java @@ -1,6 +1,7 @@ package com.song.qsrefreshlayout.refreshview; import android.content.Context; +import android.util.AttributeSet; import android.view.View; import android.view.ViewGroup; import android.widget.ImageView; @@ -17,16 +18,26 @@ public class SunRefreshView extends ImageView implements IRefreshView { int mTotalDragDistance; SunRefreshDraw plainRefreshDraw; + public SunRefreshView(Context context) { - super(context); + this(context, null); + } + + public SunRefreshView(Context context, AttributeSet attrs) { + super(context, attrs); float density = context.getResources().getDisplayMetrics().density; - mTotalDragDistance = Math.round((float) 120 * density); - setLayoutParams(new ViewGroup.LayoutParams(-1, mTotalDragDistance)); + setLayoutParams(new ViewGroup.LayoutParams(-1, mTotalDragDistance = Math.round((float) 120 * density))); plainRefreshDraw = new SunRefreshDraw(context, mTotalDragDistance); setImageDrawable(plainRefreshDraw); } + @Override//确定view大小 + protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { + super.onMeasure(widthMeasureSpec, heightMeasureSpec); + plainRefreshDraw.setGetTotalDragDistance(mTotalDragDistance = getMeasuredHeight()); + } + @Override public View getView() { return this; diff --git a/app/src/main/res/layout/activity_recyle.xml b/app/src/main/res/layout/activity_recyle.xml index 21f8025..700ac5f 100644 --- a/app/src/main/res/layout/activity_recyle.xml +++ b/app/src/main/res/layout/activity_recyle.xml @@ -11,6 +11,7 @@ android:id="@+id/qs" android:layout_width="match_parent" android:layout_height="match_parent"> +