android 简单上拉下拉刷新功能

事件处理 

@Override
    public boolean onTouch(View v, MotionEvent event) {
        float y = event.getY();
        switch (event.getAction()) {
            case MotionEvent.ACTION_MOVE:
                lastY = guanErRecydlerview.lastY;
                float offsetY = y - lastY;
                if (mCurrentPage == 0) {
                    if (offsetY >= 700) break;
                    tvLoadRefresh.setText("松手刷新");
                    loadHeader.animate().translationY(offsetY / 4).setDuration(0).start();
                } else if (mCurrentPage == scrollHelper.getPageCount() - 1 && page < maxPageNum) {
                    if (Math.abs(offsetY) >= 700) break;
                    loadFooter.animate().translationY(offsetY / 4).setDuration(0).start();
                }
                break;
            case MotionEvent.ACTION_UP:
                lastY = guanErRecydlerview.lastY;
                offsetY = y - lastY;
                if (mCurrentPage == 0) {
                    if (offsetY > 700) {
                        tvLoadRefresh.setText("加载中");
                        loadRefresh();
                        startLoadAnimation(false);
                    } else {
                        loadHeader.animate().translationY(0).setDuration(100).start();
                    }
                } else if (mCurrentPage == scrollHelper.getPageCount() - 1) {
                    if (offsetY < -700 && page < maxPageNum) {
                        loadMore();
                        startLoadAnimation(true);
                    } else {
                        loadFooter.animate().translationY(0).setDuration(100).start();
                    }
                }
                break;
        }
        return false;
    }

 

loadHeader

private Handler mHandler = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            switch (msg.what) {
                case 1000:
                    if (objectAnimator != null) {
                        objectAnimator.end();
                        loadHeader.animate().translationY(0).setDuration(100).start();
                    }
                    break;
                case 1001:
                    if (objectAnimator != null) {
                        objectAnimator.end();
                        loadFooter.animate().translationY(0).setDuration(100).start();
                    }
            }

        }
    };

hander

Api.getList(activity, token, page + "", new CallbackHttp() {
            @Override
            public void onResult(boolean isSuccess, int code, String msg, String result) {
                tvLoadRefresh.setText("下拉加载");
                Message message = new Message();
                if (isFirstLoading) {
                    message.what = 1000;
                } else {
                    message.what = 1001;
                }
                mHandler.sendMessageDelayed(message, 0);
                if (isSuccess) {
                  ............

刷新动画

private void startLoadAnimation(boolean isloadMore) {
        if (isloadMore) {
            objectAnimator = ObjectAnimator.ofFloat(ivLoadMore, "rotation", 0, 360);
        } else {
            objectAnimator = ObjectAnimator.ofFloat(ivLoadRefresh, "rotation", 0, 360);
        }

        objectAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
            }
        });
        objectAnimator.setDuration(500);
        objectAnimator.setRepeatCount(2000);
        objectAnimator.start();
    }

 

你可能感兴趣的:(android 简单上拉下拉刷新功能)