use of com.scwang.smartrefresh.layout.api.RefreshHeader in project SmartRefreshLayout by scwang90.
the class WeiboPracticeActivity method onCreate.
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_practice_weibo);
final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
// 状态栏透明和间距处理
StatusBarUtil.immersive(this);
StatusBarUtil.setPaddingSmart(this, toolbar);
final View parallax = findViewById(R.id.parallax);
final View buttonBar = findViewById(R.id.buttonBarLayout);
final NestedScrollView scrollView = (NestedScrollView) findViewById(R.id.scrollView);
final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
findViewById(R.id.attention).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "点击了关注", Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.leaveword).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Toast.makeText(view.getContext(), "点击了留言", Toast.LENGTH_SHORT).show();
}
});
refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {
@Override
public void onRefresh(@NonNull RefreshLayout refreshLayout) {
refreshLayout.finishRefresh(3000);
}
@Override
public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
refreshLayout.finishLoadMore(2000);
}
@Override
public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
mOffset = offset / 2;
parallax.setTranslationY(mOffset - mScrollY);
toolbar.setAlpha(1 - Math.min(percent, 1));
}
});
scrollView.setOnScrollChangeListener(new NestedScrollView.OnScrollChangeListener() {
private int lastScrollY = 0;
private int h = DensityUtil.dp2px(170);
private int color = ContextCompat.getColor(getApplicationContext(), R.color.colorPrimary) & 0x00ffffff;
@Override
public void onScrollChange(NestedScrollView v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (lastScrollY < h) {
scrollY = Math.min(h, scrollY);
mScrollY = scrollY > h ? h : scrollY;
buttonBar.setAlpha(1f * mScrollY / h);
toolbar.setBackgroundColor(((255 * mScrollY / h) << 24) | color);
parallax.setTranslationY(mOffset - mScrollY);
}
lastScrollY = scrollY;
}
});
buttonBar.setAlpha(0);
toolbar.setBackgroundColor(0);
}
use of com.scwang.smartrefresh.layout.api.RefreshHeader in project SmartRefreshLayout by scwang90.
the class SmartRefreshLayout method finishRefresh.
/**
* 完成刷新
* @param delayed 开始延时
* @param success 数据是否成功刷新 (会影响到上次更新时间的改变)
* @return SmartRefreshLayout
*/
@Override
public SmartRefreshLayout finishRefresh(int delayed, final boolean success) {
postDelayed(new Runnable() {
@Override
public void run() {
if (mState == RefreshState.Refreshing && mRefreshHeader != null && mRefreshContent != null) {
notifyStateChanged(RefreshState.RefreshFinish);
int startDelay = mRefreshHeader.onFinish(SmartRefreshLayout.this, success);
if (mOnMultiPurposeListener != null && mRefreshHeader instanceof RefreshHeader) {
mOnMultiPurposeListener.onHeaderFinish((RefreshHeader) mRefreshHeader, success);
}
if (startDelay < Integer.MAX_VALUE) {
if (mIsBeingDragged) {
mTouchSpinner = 0;
mTouchY = mLastTouchY;
mIsBeingDragged = false;
long time = System.currentTimeMillis();
SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_DOWN, mLastTouchX, mTouchY + mSpinner - mTouchSlop * 2, 0));
SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_MOVE, mLastTouchX, mTouchY + mSpinner, 0));
}
if (mSpinner > 0) {
AnimatorUpdateListener updateListener = null;
ValueAnimator valueAnimator = animSpinner(0, startDelay, mReboundInterpolator, mReboundDuration);
if (mEnableScrollContentWhenRefreshed) {
updateListener = mRefreshContent.scrollContentWhenFinished(mSpinner);
}
if (valueAnimator != null && updateListener != null) {
valueAnimator.addUpdateListener(updateListener);
}
} else if (mSpinner < 0) {
animSpinner(0, startDelay, mReboundInterpolator, mReboundDuration);
} else {
mKernel.moveSpinner(0, false);
resetStatus();
}
}
}
}
}, delayed <= 0 ? 1 : delayed);
return this;
}
use of com.scwang.smartrefresh.layout.api.RefreshHeader in project SmartRefreshLayout by scwang90.
the class SmartRefreshLayout method setStateRefreshing.
protected void setStateRefreshing() {
AnimatorListenerAdapter listener = new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
mLastOpenTime = currentTimeMillis();
notifyStateChanged(RefreshState.Refreshing);
if (mRefreshListener != null) {
mRefreshListener.onRefresh(SmartRefreshLayout.this);
} else if (mOnMultiPurposeListener == null) {
finishRefresh(3000);
}
if (mRefreshHeader != null) {
mRefreshHeader.onStartAnimator(SmartRefreshLayout.this, mHeaderHeight, (int) (mHeaderMaxDragRate * mHeaderHeight));
}
if (mOnMultiPurposeListener != null && mRefreshHeader instanceof RefreshHeader) {
mOnMultiPurposeListener.onRefresh(SmartRefreshLayout.this);
mOnMultiPurposeListener.onHeaderStartAnimator((RefreshHeader) mRefreshHeader, mHeaderHeight, (int) (mHeaderMaxDragRate * mHeaderHeight));
}
}
};
notifyStateChanged(RefreshState.RefreshReleased);
ValueAnimator animator = mKernel.animSpinner(mHeaderHeight);
if (animator != null) {
animator.addListener(listener);
}
if (mRefreshHeader != null) {
// onReleased 的执行顺序定在 animSpinner 之后 onAnimationEnd 之前
// 这样 onRefreshReleased内部 可以做出 对 前面 animSpinner 的覆盖 操作
mRefreshHeader.onReleased(this, mHeaderHeight, (int) (mHeaderMaxDragRate * mHeaderHeight));
}
if (mOnMultiPurposeListener != null && mRefreshHeader instanceof RefreshHeader) {
// 同 mRefreshHeader.onReleased 一致
mOnMultiPurposeListener.onHeaderReleased((RefreshHeader) mRefreshHeader, mHeaderHeight, (int) (mHeaderMaxDragRate * mHeaderHeight));
}
if (animator == null) {
// onAnimationEnd 会改变状态为 Refreshing 必须在 onReleased 之后调用
listener.onAnimationEnd(null);
}
}
use of com.scwang.smartrefresh.layout.api.RefreshHeader in project SmartRefreshLayout by scwang90.
the class SmartRefreshLayout method onFinishInflate.
// </editor-fold>
// <editor-fold desc="生命周期 life cycle">
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final int count = super.getChildCount();
if (count > 3) {
throw new RuntimeException("最多只支持3个子View,Most only support three sub view");
}
int contentLevel = 0;
int indexContent = -1;
for (int i = 0; i < count; i++) {
View view = super.getChildAt(i);
if (isScrollableView(view) && (contentLevel < 2 || i == 1)) {
indexContent = i;
contentLevel = 2;
} else if (!(view instanceof RefreshInternal) && contentLevel < 1) {
indexContent = i;
contentLevel = i > 0 ? 1 : 0;
}
}
// int[] indexArray = {1,0,2};
//
// for (int index : indexArray) {
// if (index < count) {
// View view = super.getChildAt(index);
// if (!(view instanceof RefreshInternal)) {
// indexContent = index;
// }
// if (isScrollableView(view)) {
// indexContent = index;
// break;
// }
// }
// }
int indexHeader = -1;
int indexFooter = -1;
if (indexContent >= 0) {
mRefreshContent = new RefreshContentWrapper(super.getChildAt(indexContent));
if (indexContent == 1) {
indexHeader = 0;
if (count == 3) {
indexFooter = 2;
}
} else if (count == 2) {
indexFooter = 1;
}
}
for (int i = 0; i < count; i++) {
View view = super.getChildAt(i);
if (i == indexHeader || (i != indexFooter && indexHeader == -1 && mRefreshHeader == null && view instanceof RefreshHeader)) {
mRefreshHeader = (view instanceof RefreshHeader) ? (RefreshHeader) view : new RefreshHeaderWrapper(view);
} else if (i == indexFooter || (indexFooter == -1 && view instanceof RefreshFooter)) {
mEnableLoadMore = (mEnableLoadMore || !mManualLoadMore);
mRefreshFooter = (view instanceof RefreshFooter) ? (RefreshFooter) view : new RefreshFooterWrapper(view);
// } else if (mRefreshContent == null) {
// mRefreshContent = new RefreshContentWrapper(view);
}
}
}
use of com.scwang.smartrefresh.layout.api.RefreshHeader in project SmartRefreshLayout by scwang90.
the class TwoLevelHeader method onFinishInflate.
// </editor-fold>
// <editor-fold desc="生命周期">
@Override
protected void onFinishInflate() {
super.onFinishInflate();
final ViewGroup thisGroup = this;
for (int i = 0, len = thisGroup.getChildCount(); i < len; i++) {
View childAt = thisGroup.getChildAt(i);
if (childAt instanceof RefreshHeader) {
mWrapperView = childAt;
mRefreshHeader = (RefreshHeader) childAt;
thisGroup.bringChildToFront(childAt);
break;
}
}
if (mRefreshHeader == null) {
mRefreshHeader = new RefreshHeaderWrapper(this);
}
}
Aggregations