Search in sources :

Example 1 with RefreshFooter

use of com.scwang.smartrefresh.layout.api.RefreshFooter in project SmartRefreshLayout by scwang90.

the class SmartRefreshLayout method finishLoadMore.

/**
 * 完成加载
 * @param delayed 开始延时
 * @param success 数据是否成功
 * @param noMoreData 是否有更多数据
 * @return SmartRefreshLayout
 */
@Override
public SmartRefreshLayout finishLoadMore(int delayed, final boolean success, final boolean noMoreData) {
    postDelayed(new Runnable() {

        @Override
        public void run() {
            if (mState == RefreshState.Loading && mRefreshFooter != null && mRefreshContent != null) {
                notifyStateChanged(RefreshState.LoadFinish);
                final int startDelay = mRefreshFooter.onFinish(SmartRefreshLayout.this, success);
                if (mOnMultiPurposeListener != null && mRefreshFooter instanceof RefreshFooter) {
                    mOnMultiPurposeListener.onFooterFinish((RefreshFooter) mRefreshFooter, success);
                }
                if (startDelay < Integer.MAX_VALUE) {
                    // 计算布局将要移动的偏移量
                    final boolean needHoldFooter = noMoreData && mEnableFooterFollowWhenLoadFinished && mSpinner < 0 && mRefreshContent.canLoadMore();
                    final int offset = mSpinner - (needHoldFooter ? Math.max(mSpinner, -mFooterHeight) : 0);
                    // 如果正在拖动的话,偏移初始点击事件
                    if (mIsBeingDragged) {
                        mTouchSpinner = mSpinner - offset;
                        mTouchY = mLastTouchY;
                        mIsBeingDragged = false;
                        final long time = System.currentTimeMillis();
                        SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_DOWN, mLastTouchX, mTouchY + offset + mTouchSlop * 2, 0));
                        SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_MOVE, mLastTouchX, mTouchY + offset, 0));
                    } else if (mTotalUnconsumed != 0) {
                        mDragDirection = 'h';
                        mTotalUnconsumed = 0;
                        final long time = System.currentTimeMillis();
                        SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_CANCEL, mLastTouchX, mTouchY + offset, 0));
                    // SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_DOWN, mLastTouchX, mTouchY + offset + mTouchSlop * 2, 0));
                    // SmartRefreshLayout.super.dispatchTouchEvent(obtain(time, time, MotionEvent.ACTION_MOVE, mLastTouchX, mTouchY + offset, 0));
                    }
                    // 准备:偏移并结束状态
                    postDelayed(new Runnable() {

                        @Override
                        public void run() {
                            AnimatorUpdateListener updateListener = null;
                            if (mEnableScrollContentWhenLoaded && offset < 0) {
                                updateListener = mRefreshContent.scrollContentWhenFinished(mSpinner);
                            }
                            if (updateListener != null) {
                                updateListener.onAnimationUpdate(ValueAnimator.ofInt(0, 0));
                            }
                            ValueAnimator animator = null;
                            AnimatorListenerAdapter listenerAdapter = new AnimatorListenerAdapter() {

                                @Override
                                public void onAnimationCancel(Animator animation) {
                                    super.onAnimationEnd(animation);
                                }

                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    mFooterLocked = false;
                                    if (noMoreData) {
                                        setNoMoreData(true);
                                    }
                                    if (mState == RefreshState.LoadFinish) {
                                        notifyStateChanged(RefreshState.None);
                                    }
                                }
                            };
                            if (mSpinner > 0) {
                                animator = mKernel.animSpinner(0);
                            } else if (updateListener != null || mSpinner == 0) {
                                if (reboundAnimator != null) {
                                    reboundAnimator.cancel();
                                    reboundAnimator = null;
                                }
                                mKernel.moveSpinner(0, false);
                                resetStatus();
                            } else {
                                if (noMoreData && mEnableFooterFollowWhenLoadFinished) {
                                    if (mSpinner >= -mFooterHeight) {
                                        notifyStateChanged(RefreshState.None);
                                    } else {
                                        animator = mKernel.animSpinner(-mFooterHeight);
                                    }
                                } else {
                                    animator = mKernel.animSpinner(0);
                                }
                            }
                            if (animator != null) {
                                animator.addListener(listenerAdapter);
                            } else {
                                listenerAdapter.onAnimationEnd(null);
                            }
                        }
                    }, mSpinner < 0 ? startDelay : 0);
                }
            } else {
                if (noMoreData) {
                    setNoMoreData(true);
                }
            }
        }
    }, delayed <= 0 ? 1 : delayed);
    return this;
}
Also used : RefreshFooter(com.scwang.smartrefresh.layout.api.RefreshFooter) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) DelayedRunnable(com.scwang.smartrefresh.layout.util.DelayedRunnable) AnimatorUpdateListener(android.animation.ValueAnimator.AnimatorUpdateListener) ValueAnimator(android.animation.ValueAnimator)

Example 2 with RefreshFooter

use of com.scwang.smartrefresh.layout.api.RefreshFooter in project SmartRefreshLayout by scwang90.

the class AssignDefaultExampleActivity method install.

/*
     * 关键代码,需要在布局生成之前设置,建议代码放在 Application 中
     */
private static void install() {
    // 设置全局的Header构建器
    SmartRefreshLayout.setDefaultRefreshHeaderCreator(new DefaultRefreshHeaderCreator() {

        @NonNull
        @Override
        public RefreshHeader createRefreshHeader(@NonNull Context context, @NonNull RefreshLayout layout) {
            ClassicsHeader header = new ClassicsHeader(context).setSpinnerStyle(SpinnerStyle.FixedBehind);
            header.setPrimaryColorId(R.color.colorPrimary);
            header.setAccentColorId(android.R.color.white);
            // 指定为经典Header,默认是 贝塞尔雷达Header
            return header;
        }
    });
    // 设置全局的Footer构建器
    SmartRefreshLayout.setDefaultRefreshFooterCreator(new DefaultRefreshFooterCreator() {

        @NonNull
        @Override
        public RefreshFooter createRefreshFooter(@NonNull Context context, @NonNull RefreshLayout layout) {
            // 内容不满一页时候启用加载更多
            layout.setEnableLoadMoreWhenContentNotFull(true);
            ClassicsFooter footer = new ClassicsFooter(context);
            footer.setBackgroundResource(android.R.color.white);
            // 设置为拉伸模式
            footer.setSpinnerStyle(SpinnerStyle.Scale);
            // 指定为经典Footer,默认是 BallPulseFooter
            return footer;
        }
    });
}
Also used : Context(android.content.Context) RefreshHeader(com.scwang.smartrefresh.layout.api.RefreshHeader) RefreshFooter(com.scwang.smartrefresh.layout.api.RefreshFooter) DefaultRefreshFooterCreator(com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator) DefaultRefreshHeaderCreator(com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator) SmartRefreshLayout(com.scwang.smartrefresh.layout.SmartRefreshLayout) RefreshLayout(com.scwang.smartrefresh.layout.api.RefreshLayout) ClassicsHeader(com.scwang.smartrefresh.layout.header.ClassicsHeader) NonNull(android.support.annotation.NonNull) ClassicsFooter(com.scwang.smartrefresh.layout.footer.ClassicsFooter)

Example 3 with RefreshFooter

use of com.scwang.smartrefresh.layout.api.RefreshFooter in project SmartRefreshLayout by scwang90.

the class ListenerExampleActivity method onCreate.

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_example_listener);
    final Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    toolbar.setNavigationOnClickListener(new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            finish();
        }
    });
    mTvContent = (TextView) findViewById(R.id.content);
    mTvContent.setMovementMethod(ScrollingMovementMethod.getInstance());
    final RefreshLayout refreshLayout = (RefreshLayout) findViewById(R.id.refreshLayout);
    refreshLayout.setOnMultiPurposeListener(new SimpleMultiPurposeListener() {

        @Override
        public void onHeaderMoving(RefreshHeader header, boolean isDragging, float percent, int offset, int headerHeight, int maxDragHeight) {
            mHeaderMoving = String.format(Locale.CHINA, "%s\nisDragging=%b,percent=%.02f offset=%03d\nheight=%03d extend=%03d", FORMAT.format(new Date()), isDragging, percent, offset, headerHeight, maxDragHeight);
            updateContent();
        }

        // @Override
        // public void onHeaderPulling(@NonNull RefreshHeader header, float percent, int offset, int headerHeight, int maxDragHeight) {
        // mHeaderPulling = String.format(Locale.CHINA, "%s\npercent=%.02f offset=%03d\nheight=%03d extend=%03d",
        // FORMAT.format(new Date()),
        // percent,offset,headerHeight,maxDragHeight);
        // updateContent();
        // }
        // 
        // @Override
        // public void onHeaderReleasing(@NonNull RefreshHeader header, float percent, int offset, int headerHeight, int maxDragHeight) {
        // mHeaderReleasing = String.format(Locale.CHINA, "%s\npercent=%.02f offset=%03d\nheight=%03d extend=%03d",
        // FORMAT.format(new Date()),
        // percent,offset,headerHeight,maxDragHeight);
        // updateContent();
        // }
        @Override
        public void onHeaderStartAnimator(@NonNull RefreshHeader header, int headerHeight, int maxDragHeight) {
            mHeaderStartAnimator = String.format(Locale.CHINA, "%s\nheight=%03d extend=%03d", FORMAT.format(new Date()), headerHeight, maxDragHeight);
            updateContent();
        }

        @Override
        public void onHeaderFinish(@NonNull RefreshHeader header, boolean success) {
            mHeaderFinish = String.format(Locale.CHINA, "%s - " + success, FORMAT.format(new Date()));
            updateContent();
        }

        @Override
        public void onFooterMoving(RefreshFooter footer, boolean isDragging, float percent, int offset, int footerHeight, int maxDragHeight) {
            mFooterMoving = String.format(Locale.CHINA, "%s\nisDragging=%b,percent=%.02f offset=%03d\nheight=%03d extend=%03d", FORMAT.format(new Date()), isDragging, percent, offset, footerHeight, maxDragHeight);
            updateContent();
        }

        // @Override
        // public void onFooterPulling(@NonNull RefreshFooter footer, float percent, int offset, int footerHeight, int maxDragHeight) {
        // mFooterPulling = String.format(Locale.CHINA, "%s\npercent=%.02f\noffset=%03d height=%03d\nextend=%03d",
        // FORMAT.format(new Date()),
        // percent,offset,footerHeight,maxDragHeight);
        // updateContent();
        // }
        // 
        // @Override
        // public void onFooterReleasing(@NonNull RefreshFooter footer, float percent, int offset, int footerHeight, int maxDragHeight) {
        // mFooterReleasing = String.format(Locale.CHINA, "%s\npercent=%.02f\noffset=%03d height=%03d\nextend=%03d",
        // FORMAT.format(new Date()),
        // percent,offset,footerHeight,maxDragHeight);
        // updateContent();
        // }
        @Override
        public void onFooterStartAnimator(@NonNull RefreshFooter footer, int footerHeight, int maxDragHeight) {
            mFooterStartAnimator = String.format(Locale.CHINA, "%s\nheight=%03d extend=%03d", FORMAT.format(new Date()), footerHeight, maxDragHeight);
            updateContent();
        }

        @Override
        public void onFooterFinish(@NonNull RefreshFooter footer, boolean success) {
            mFooterFinish = String.format(Locale.CHINA, "%s - " + success, FORMAT.format(new Date()));
            updateContent();
        }

        @Override
        public void onRefresh(@NonNull RefreshLayout refreshLayout) {
            mRefresh = String.format(Locale.CHINA, "%s", FORMAT.format(new Date()));
            updateContent();
            refreshLayout.finishRefresh(2000);
        }

        @Override
        public void onLoadMore(@NonNull RefreshLayout refreshLayout) {
            mLoadMore = String.format(Locale.CHINA, "%s", FORMAT.format(new Date()));
            updateContent();
            refreshLayout.finishLoadMore(1000);
        }

        @Override
        public void onStateChanged(@NonNull RefreshLayout refreshLayout, @NonNull RefreshState oldState, @NonNull RefreshState newState) {
            mStateChanged = String.format(Locale.CHINA, "%s\nnew=%s\nold=%s", FORMAT.format(new Date()), newState.name(), oldState.name());
            updateContent();
        }
    });
    if (isFirstEnter) {
        isFirstEnter = false;
        // 触发自动刷新
        refreshLayout.autoRefresh();
    } else {
        updateContent();
    }
}
Also used : RefreshHeader(com.scwang.smartrefresh.layout.api.RefreshHeader) RefreshFooter(com.scwang.smartrefresh.layout.api.RefreshFooter) SimpleMultiPurposeListener(com.scwang.smartrefresh.layout.listener.SimpleMultiPurposeListener) TextView(android.widget.TextView) View(android.view.View) Date(java.util.Date) RefreshLayout(com.scwang.smartrefresh.layout.api.RefreshLayout) RefreshState(com.scwang.smartrefresh.layout.constant.RefreshState) Toolbar(android.support.v7.widget.Toolbar)

Example 4 with RefreshFooter

use of com.scwang.smartrefresh.layout.api.RefreshFooter in project SmartRefreshLayout by scwang90.

the class SmartRefreshLayout method setStateLoading.

protected void setStateLoading() {
    AnimatorListenerAdapter listener = new AnimatorListenerAdapter() {

        @Override
        public void onAnimationEnd(Animator animation) {
            setStateDirectLoading();
        }
    };
    notifyStateChanged(RefreshState.LoadReleased);
    ValueAnimator animator = mKernel.animSpinner(-mFooterHeight);
    if (animator != null) {
        animator.addListener(listener);
    }
    if (mRefreshFooter != null) {
        // onReleased 的执行顺序定在 animSpinner 之后 onAnimationEnd 之前
        // 这样 onReleased 内部 可以做出 对 前面 animSpinner 的覆盖 操作
        mRefreshFooter.onReleased(this, mFooterHeight, (int) (mFooterMaxDragRate * mFooterHeight));
    }
    if (mOnMultiPurposeListener != null && mRefreshFooter instanceof RefreshFooter) {
        // 同 mRefreshFooter.onReleased 一致
        mOnMultiPurposeListener.onFooterReleased((RefreshFooter) mRefreshFooter, mFooterHeight, (int) (mFooterMaxDragRate * mFooterHeight));
    }
    if (animator == null) {
        // onAnimationEnd 会改变状态为 loading 必须在 onReleased 之后调用
        listener.onAnimationEnd(null);
    }
}
Also used : RefreshFooter(com.scwang.smartrefresh.layout.api.RefreshFooter) Animator(android.animation.Animator) ValueAnimator(android.animation.ValueAnimator) AnimatorListenerAdapter(android.animation.AnimatorListenerAdapter) ValueAnimator(android.animation.ValueAnimator)

Example 5 with RefreshFooter

use of com.scwang.smartrefresh.layout.api.RefreshFooter 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);
        }
    }
}
Also used : RefreshInternal(com.scwang.smartrefresh.layout.api.RefreshInternal) RefreshHeaderWrapper(com.scwang.smartrefresh.layout.impl.RefreshHeaderWrapper) RefreshHeader(com.scwang.smartrefresh.layout.api.RefreshHeader) RefreshFooter(com.scwang.smartrefresh.layout.api.RefreshFooter) RefreshFooterWrapper(com.scwang.smartrefresh.layout.impl.RefreshFooterWrapper) RefreshContentWrapper(com.scwang.smartrefresh.layout.impl.RefreshContentWrapper) View(android.view.View) TextView(android.widget.TextView) SmartUtil.isScrollableView(com.scwang.smartrefresh.layout.util.SmartUtil.isScrollableView) Paint(android.graphics.Paint) SuppressLint(android.annotation.SuppressLint)

Aggregations

RefreshFooter (com.scwang.smartrefresh.layout.api.RefreshFooter)6 RefreshHeader (com.scwang.smartrefresh.layout.api.RefreshHeader)4 RefreshLayout (com.scwang.smartrefresh.layout.api.RefreshLayout)3 Animator (android.animation.Animator)2 AnimatorListenerAdapter (android.animation.AnimatorListenerAdapter)2 ValueAnimator (android.animation.ValueAnimator)2 Context (android.content.Context)2 NonNull (android.support.annotation.NonNull)2 View (android.view.View)2 TextView (android.widget.TextView)2 SmartRefreshLayout (com.scwang.smartrefresh.layout.SmartRefreshLayout)2 DefaultRefreshFooterCreator (com.scwang.smartrefresh.layout.api.DefaultRefreshFooterCreator)2 DefaultRefreshHeaderCreator (com.scwang.smartrefresh.layout.api.DefaultRefreshHeaderCreator)2 AnimatorUpdateListener (android.animation.ValueAnimator.AnimatorUpdateListener)1 SuppressLint (android.annotation.SuppressLint)1 Paint (android.graphics.Paint)1 Toolbar (android.support.v7.widget.Toolbar)1 RefreshInternal (com.scwang.smartrefresh.layout.api.RefreshInternal)1 RefreshState (com.scwang.smartrefresh.layout.constant.RefreshState)1 ClassicsFooter (com.scwang.smartrefresh.layout.footer.ClassicsFooter)1