Skip to content

Commit f4e3b07

Browse files
committed
优化Header
1 parent 85bfacf commit f4e3b07

File tree

6 files changed

+71
-47
lines changed

6 files changed

+71
-47
lines changed

app/src/main/res/layout/fragment_refresh_practive.xml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,8 @@
3131
tools:listitem="@android:layout/simple_list_item_2"/>
3232
<com.scwang.smartrefresh.header.StoreHouseHeader
3333
android:layout_width="match_parent"
34-
android:layout_height="wrap_content"/>
34+
android:layout_height="wrap_content"
35+
app:shhEnableFadeAnimation="true"/>
3536
</com.scwang.smartrefresh.layout.SmartRefreshLayout>
3637

3738
</LinearLayout>

refresh-header/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ publish {
4040
userOrg = 'scwang90'
4141
groupId = 'com.scwang.smartrefresh'
4242
artifactId = 'SmartRefreshHeader'
43-
version = '1.0.5-alpha-3'
43+
version = '1.0.5'
4444
description = 'Some delicate headers of SmartRefreshLayout'
4545
website = "https://github.com/scwang90/${rootProject.name}"
4646
}

refresh-header/src/main/java/com/scwang/smartrefresh/header/StoreHouseHeader.java

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
import android.support.annotation.RequiresApi;
1515
import android.util.AttributeSet;
1616
import android.view.View;
17+
import android.view.animation.AccelerateInterpolator;
18+
import android.view.animation.Animation;
1719
import android.view.animation.Transformation;
1820

1921
import com.scwang.smartrefresh.header.storehouse.StoreHouseBarItem;
@@ -61,6 +63,7 @@ public class StoreHouseHeader extends View implements RefreshHeader {
6163
private int mTextColor = Color.WHITE;
6264
private int mBackgroundColor = 0;
6365
private boolean mIsInLoading = false;
66+
private boolean mEnableFadeAnimation = false;
6467
private Matrix mMatrix = new Matrix();
6568
private RefreshKernel mRefreshKernel;
6669
private AniController mAniController = new AniController();
@@ -100,6 +103,7 @@ private void initView(Context context, AttributeSet attrs) {
100103
TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.StoreHouseHeader);
101104
mLineWidth = ta.getDimensionPixelOffset(R.styleable.StoreHouseHeader_shhLineWidth, mLineWidth);
102105
mDropHeight = ta.getDimensionPixelOffset(R.styleable.StoreHouseHeader_shhDropHeight, mDropHeight);
106+
mEnableFadeAnimation = ta.getBoolean(R.styleable.StoreHouseHeader_shhEnableFadeAnimation, mEnableFadeAnimation);
103107
if (ta.hasValue(R.styleable.StoreHouseHeader_shhText)) {
104108
initWithString(ta.getString(R.styleable.StoreHouseHeader_shhText));
105109
} else {
@@ -125,13 +129,9 @@ protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
125129
@Override
126130
public void onDraw(Canvas canvas) {
127131
super.onDraw(canvas);
128-
float progress = mProgress;
129-
int c1 = canvas.save();
130-
int len = mItemList.size();
131-
132-
if (isInEditMode()) {
133-
progress = 1;
134-
}
132+
final int c1 = canvas.save();
133+
final int len = mItemList.size();
134+
final float progress = isInEditMode() ? 1 : mProgress;
135135

136136
for (int i = 0; i < len; i++) {
137137

@@ -360,8 +360,27 @@ public void onStateChanged(RefreshLayout refreshLayout, RefreshState oldState, R
360360
@Override
361361
public int onFinish(@NonNull RefreshLayout layout, boolean success) {
362362
loadFinish();
363-
for (int i = 0; i < mItemList.size(); i++) {
364-
mItemList.get(i).resetPosition(mHorizontalRandomness);
363+
if (success && mEnableFadeAnimation) {
364+
startAnimation(new Animation() {{
365+
setDuration(250);
366+
setInterpolator(new AccelerateInterpolator());
367+
}
368+
@Override
369+
protected void applyTransformation(float interpolatedTime, Transformation t) {
370+
setProgress(1 - interpolatedTime);
371+
invalidate();
372+
if (interpolatedTime == 1) {
373+
for (int i = 0; i < mItemList.size(); i++) {
374+
mItemList.get(i).resetPosition(mHorizontalRandomness);
375+
}
376+
}
377+
}
378+
});
379+
return 250;
380+
} else {
381+
for (int i = 0; i < mItemList.size(); i++) {
382+
mItemList.get(i).resetPosition(mHorizontalRandomness);
383+
}
365384
}
366385
return 0;
367386
}

refresh-header/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@
6464
<attr name="shhText" format="string" />
6565
<attr name="shhLineWidth" format="dimension" />
6666
<attr name="shhDropHeight" format="dimension" />
67+
<attr name="shhEnableFadeAnimation" format="boolean" />
6768
</declare-styleable>
6869

6970

refresh-layout/build.gradle

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ publish {
3939
userOrg = 'scwang90'
4040
groupId = 'com.scwang.smartrefresh'
4141
artifactId = 'SmartRefreshLayout'
42-
version = '1.0.5-alpha-3'
42+
version = '1.0.5'
4343
description = 'An intelligent refresh layout'
4444
website = "https://github.com/scwang90/${rootProject.name}"
4545
}

refresh-layout/src/main/java/com/scwang/smartrefresh/layout/SmartRefreshLayout.java

Lines changed: 38 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -848,15 +848,11 @@ public void computeScroll() {
848848
if(mVerticalPermit) {
849849
float velocity;
850850
if (Build.VERSION.SDK_INT >= 14) {
851-
velocity = mScroller.getCurrVelocity();
851+
velocity = finalY > 0 ? -mScroller.getCurrVelocity() : mScroller.getCurrVelocity();
852852
} else {
853-
velocity = 1f * (finalY - mScroller.getCurrY()) / Math.max((mScroller.getDuration() - mScroller.timePassed()), 1);
854-
}
855-
if (finalY > 0) {// 手势向上划 Footer
856-
animSpinnerBounce(-velocity);
857-
} else if (finalY < 0){// 手势向下划 Header
858-
animSpinnerBounce(velocity);
853+
velocity = 1f * (mScroller.getCurrY() - finalY) / Math.max((mScroller.getDuration() - mScroller.timePassed()), 1);
859854
}
855+
animSpinnerBounce(velocity);
860856
}
861857
mScroller.forceFinished(true);
862858
} else {
@@ -1734,10 +1730,16 @@ public LayoutParams(ViewGroup.LayoutParams source) {
17341730
//<editor-fold desc="嵌套滚动 NestedScrolling">
17351731

17361732
//<editor-fold desc="NestedScrollingParent">
1733+
1734+
@Override
1735+
public int getNestedScrollAxes() {
1736+
return mNestedScrollingParentHelper.getNestedScrollAxes();
1737+
}
1738+
17371739
@Override
17381740
public boolean onStartNestedScroll(@NonNull View child, @NonNull View target, int nestedScrollAxes) {
17391741
boolean accepted = isEnabled() && isNestedScrollingEnabled() && (nestedScrollAxes & ViewCompat.SCROLL_AXIS_VERTICAL) != 0;
1740-
accepted = accepted && (isEnableRefresh() || isEnableLoadMore());
1742+
accepted = accepted && (mEnableOverScrollDrag || isEnableRefresh() || isEnableLoadMore());
17411743
return accepted;
17421744
}
17431745

@@ -1883,23 +1885,6 @@ public void onNestedPreScroll(@NonNull View target, int dx, int dy, @NonNull int
18831885

18841886
}
18851887

1886-
@Override
1887-
public int getNestedScrollAxes() {
1888-
return mNestedScrollingParentHelper.getNestedScrollAxes();
1889-
}
1890-
1891-
@Override
1892-
public void onStopNestedScroll(@NonNull View target) {
1893-
mNestedScrollingParentHelper.onStopNestedScroll(target);
1894-
mNestedScrollInProgress = false;
1895-
// Finish the spinner for nested scrolling if we ever consumed any
1896-
// unconsumed nested scroll
1897-
mTotalUnconsumed = 0;
1898-
overSpinner();
1899-
// Dispatch up our nested parent
1900-
stopNestedScroll();
1901-
}
1902-
19031888
@Override
19041889
public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed) {
19051890
// Dispatch up to the nested parent first
@@ -1911,19 +1896,25 @@ public void onNestedScroll(@NonNull View target, int dxConsumed, int dyConsumed,
19111896
// 'offset in window 'functionality to see if we have been moved from the event.
19121897
// This is a decent indication of whether we should take over the event stream or not.
19131898
final int dy = dyUnconsumed + mParentOffsetInWindow[1];
1914-
if (dy < 0 && isEnableRefresh() /* && (mRefreshContent == null || mRefreshContent.canRefresh())*/) {
1915-
if (mViceState == RefreshState.None) {
1916-
mKernel.setState(RefreshState.PullDownToRefresh);
1917-
}
1918-
mTotalUnconsumed += Math.abs(dy);
1919-
moveSpinnerInfinitely(mTotalUnconsumed/* + mTouchSpinner*/);
1920-
} else if (dy > 0 && isEnableLoadMore() /* && (mRefreshContent == null || mRefreshContent.canLoadMore())*/) {
1899+
if (dy != 0 && (mEnableOverScrollDrag || (dy < 0 && isEnableRefresh()) || (dy > 0 && isEnableLoadMore()))) {
19211900
if (mViceState == RefreshState.None) {
1922-
mKernel.setState(RefreshState.PullUpToLoad);
1901+
mKernel.setState(dy > 0 ? RefreshState.PullUpToLoad : RefreshState.PullDownToRefresh);
19231902
}
1924-
mTotalUnconsumed -= Math.abs(dy);
1925-
moveSpinnerInfinitely(mTotalUnconsumed/* + mTouchSpinner*/);
1903+
moveSpinnerInfinitely(mTotalUnconsumed -= dy);
19261904
}
1905+
// if (dy < 0 && isEnableRefresh() /* && (mRefreshContent == null || mRefreshContent.canRefresh())*/) {
1906+
// if (mViceState == RefreshState.None) {
1907+
// mKernel.setState(RefreshState.PullDownToRefresh);
1908+
// }
1909+
// mTotalUnconsumed += Math.abs(dy);
1910+
// moveSpinnerInfinitely(mTotalUnconsumed/* + mTouchSpinner*/);
1911+
// } else if (dy > 0 && isEnableLoadMore() /* && (mRefreshContent == null || mRefreshContent.canLoadMore())*/) {
1912+
// if (mViceState == RefreshState.None) {
1913+
// mKernel.setState(RefreshState.PullUpToLoad);
1914+
// }
1915+
// mTotalUnconsumed -= Math.abs(dy);
1916+
// moveSpinnerInfinitely(mTotalUnconsumed/* + mTouchSpinner*/);
1917+
// }
19271918
}
19281919

19291920
@Override
@@ -1935,6 +1926,18 @@ public boolean onNestedPreFling(@NonNull View target, float velocityX, float vel
19351926
public boolean onNestedFling(@NonNull View target, float velocityX, float velocityY, boolean consumed) {
19361927
return dispatchNestedFling(velocityX, velocityY, consumed);
19371928
}
1929+
1930+
@Override
1931+
public void onStopNestedScroll(@NonNull View target) {
1932+
mNestedScrollingParentHelper.onStopNestedScroll(target);
1933+
mNestedScrollInProgress = false;
1934+
// Finish the spinner for nested scrolling if we ever consumed any
1935+
// unconsumed nested scroll
1936+
mTotalUnconsumed = 0;
1937+
overSpinner();
1938+
// Dispatch up our nested parent
1939+
stopNestedScroll();
1940+
}
19381941
//</editor-fold>
19391942

19401943
//<editor-fold desc="NestedScrollingChild">

0 commit comments

Comments
 (0)