com.linsq.androiddemo.refresh2.SwipeRefreshLayout.java Source code

Java tutorial

Introduction

Here is the source code for com.linsq.androiddemo.refresh2.SwipeRefreshLayout.java

Source

/*
 * Copyright (C) 2013 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.linsq.androiddemo.refresh2;

import android.content.Context;
import android.content.res.Resources;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.support.v4.view.MotionEventCompat;
import android.support.v4.view.ViewCompat;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.animation.AccelerateInterpolator;
import android.view.animation.Animation;
import android.view.animation.Animation.AnimationListener;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.Transformation;
import android.widget.AbsListView;

import com.linsq.androiddemo.util.Logger;

/**
 * The SwipeRefreshLayout should be used whenever the user can refresh the
 * contents of a view via a vertical swipe gesture. The activity that
 * instantiates this view should add an OnRefreshListener to be notified
 * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout
 * will notify the listener each and every time the gesture is completed again;
 * the listener is responsible for correctly determining when to actually
 * initiate a refresh of its content. If the listener determines there should
 * not be a refresh, it must call setRefreshing(false) to cancel any visual
 * indication of a refresh. If an activity wishes to show just the progress
 * animation, it should call setRefreshing(true). To disable the gesture and
 * progress animation, call setEnabled(false) on the view.
 * 
 * <p>
 * This layout should be made the parent of the view that will be refreshed as a
 * result of the gesture and can only support one direct child. This view will
 * also be made the target of the gesture and will be forced to match both the
 * width and the height supplied in this layout. The SwipeRefreshLayout does not
 * provide accessibility events; instead, a menu item must be provided to allow
 * refresh of the content wherever this gesture is used.
 * </p>
 */
public class SwipeRefreshLayout extends ViewGroup {

    private static final String Logger_TAG = SwipeRefreshLayout.class.getSimpleName();
    private static final long RETURN_TO_ORIGINAL_POSITION_TIMEOUT = 300;// ?
    private static final float MAX_SWIPE_DISTANCE_FACTOR = .6f;// view?120dpmDistanceToTriggerSync=view0.6?
    private static final int REFRESH_TRIGGER_DISTANCE = 200;// view120dpmDistanceToTriggerSync=120dp
    private float mDistanceToTriggerSync = -1;// ??
    //
    private static final int INVALID_POINTER = -1;
    private View mTarget; // ?viewViewGroup???View
    private int mOriginalOffsetTop;// ??view?=mTarget.getTop+viewgorup.paddingTop.
    private int mCurrentTargetOffsetTop;// ?view??=mTarget.getTop
    //
    private OnRefreshListener mListener;// 
    private boolean mRefreshing = false;// ??
    private int mTouchSlop;// ??
    //
    private boolean mReturningToStart;// ?
    private final DecelerateInterpolator mDecelerateInterpolator;// ?-
    private final AccelerateInterpolator mAccelerateInterpolator;// ?--
    private int mMediumAnimationDuration;// ?view&?
    //
    private SwipeProgressBar mProgressBar;
    private int mProgressBarHeight;// ?
    private boolean mIsBeingDragged;// ?
    private static final int[] LAYOUT_ATTRS = new int[] { android.R.attr.enabled };
    // ------------------------------------------------------------
    private float mFromPercentage = 0;// progressbar
    private float mCurrPercentage = 0;// ??
    // ?view?progressbar
    private final Runnable mCancel = new Runnable() {

        @Override
        public void run() {
            mReturningToStart = true;
            if (mProgressBar != null) {
                // 
                // Logger.e("mCurrPercentage=" + mCurrPercentage);
                mShrinkTrigger.reset();
                mFromPercentage = mCurrPercentage;
                mShrinkTrigger.setDuration(mMediumAnimationDuration);
                mShrinkTrigger.setAnimationListener(mShrinkAnimationListener);
                mShrinkTrigger.setInterpolator(mDecelerateInterpolator);
                startAnimation(mShrinkTrigger);
            }
            animateOffsetToStartPosition(mCurrentTargetOffsetTop + getPaddingTop(), mReturnToStartPositionListener);
        }
    };
    private Animation mShrinkTrigger = new Animation() {

        // 
        @Override
        public void applyTransformation(float interpolatedTime, Transformation t) {
            float percent = mFromPercentage + ((0 - mFromPercentage) * interpolatedTime);
            Logger.e("percent=" + percent);
            mProgressBar.setTriggerPercentage(percent);
        }
    };
    private final AnimationListener mShrinkAnimationListener = new BaseAnimationListener() {

        @Override
        public void onAnimationEnd(Animation animation) {
            mCurrPercentage = 0;
        }
    };
    // ---------------------------------------------------------------------------------
    // ?view?runnable
    private final Runnable mReturnToStartPosition = new Runnable() {

        @Override
        public void run() {
            mReturningToStart = true;
            animateOffsetToStartPosition(mCurrentTargetOffsetTop + getPaddingTop(), mReturnToStartPositionListener);
        }
    };

    // ?view??
    private void animateOffsetToStartPosition(int from, AnimationListener listener) {
        // Logger.e("mFrom=" + mFrom);
        mFrom = from;
        mAnimateToStartPosition.reset();
        mAnimateToStartPosition.setDuration(mMediumAnimationDuration);
        mAnimateToStartPosition.setAnimationListener(listener);
        mAnimateToStartPosition.setInterpolator(mDecelerateInterpolator);
        mTarget.startAnimation(mAnimateToStartPosition);
    }

    private int mFrom;// ?view?
    // view??
    private final Animation mAnimateToStartPosition = new Animation() {

        @Override
        public void applyTransformation(float interpolatedTime, Transformation t) {
            // interpolatedTime0~1?
            int targetTop = 0;
            if (mFrom != mOriginalOffsetTop) {
                targetTop = (mFrom + (int) ((mOriginalOffsetTop - mFrom) * interpolatedTime));// ?
            }
            // Logger.e("---------"+mTarget.getTop()+"="+mCurrentTargetOffsetTop);
            int offset = targetTop - mTarget.getTop();// mCurrentTargetOffsetTop;
            final int currentTop = mTarget.getTop();// mCurrentTargetOffsetTop
            if (offset + currentTop < 0) {
                offset = 0 - currentTop;
            }
            setTargetOffsetTopAndBottom(offset);
        }
    };
    private final AnimationListener mReturnToStartPositionListener = new BaseAnimationListener() {

        // ?
        @Override
        public void onAnimationEnd(Animation animation) {
            mCurrentTargetOffsetTop = 0;
        }
    };

    // --------------------------------------------------------------------------
    public SwipeRefreshLayout(Context context) {
        this(context, null);
    }

    public SwipeRefreshLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
        // 
        mMediumAnimationDuration = getResources().getInteger(android.R.integer.config_mediumAnimTime);
        setWillNotDraw(false);// view??true??false
        mProgressBar = new SwipeProgressBar(this);
        final DisplayMetrics metrics = getResources().getDisplayMetrics();
        mProgressBarHeight = (int) (metrics.density * 4);// ?
        mDecelerateInterpolator = new DecelerateInterpolator(2f);
        mAccelerateInterpolator = new AccelerateInterpolator(1.5f);
        final TypedArray a = context.obtainStyledAttributes(attrs, LAYOUT_ATTRS);
        setEnabled(a.getBoolean(0, true));
        a.recycle();
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        removeCallbacks(mCancel);
        removeCallbacks(mReturnToStartPosition);
    }

    @Override
    public void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        removeCallbacks(mReturnToStartPosition);
        removeCallbacks(mCancel);
    }

    /**
     * @deprecated Use {@link #setColorSchemeResources(int, int, int, int)}
     */
    @Deprecated
    public void setColorScheme(int colorRes1, int colorRes2, int colorRes3, int colorRes4) {
        setColorSchemeResources(colorRes1, colorRes2, colorRes3, colorRes4);
    }

    /**
     * Set the four colors used in the progress animation from color resources.
     * The first color will also be the color of the bar that grows in response
     * to a user swipe gesture.
     */
    public void setColorSchemeResources(int colorRes1, int colorRes2, int colorRes3, int colorRes4) {
        final Resources res = getResources();
        setColorSchemeColors(res.getColor(colorRes1), res.getColor(colorRes2), res.getColor(colorRes3),
                res.getColor(colorRes4));
    }

    /**
     * Set the four colors used in the progress animation. The first color will
     * also be the color of the bar that grows in response to a user swipe
     * gesture.
     */
    public void setColorSchemeColors(int color1, int color2, int color3, int color4) {
        ensureTarget();
        mProgressBar.setColorScheme(color1, color2, color3, color4);
    }

    // ?view?
    private void ensureTarget() {
        // ????
        if (mTarget == null) {
            if (getChildCount() > 1 && !isInEditMode()) {
                // ???View
                throw new IllegalStateException("SwipeRefreshLayout can host only one direct child");
            }
            mTarget = getChildAt(0);
            mOriginalOffsetTop = mTarget.getTop() + getPaddingTop();
        }
        if (mDistanceToTriggerSync == -1) {
            if (getParent() != null && ((View) getParent()).getHeight() > 0) {
                final DisplayMetrics metrics = getResources().getDisplayMetrics();
                mDistanceToTriggerSync = (int) Math.min(
                        ((View) getParent()).getHeight() * MAX_SWIPE_DISTANCE_FACTOR,
                        REFRESH_TRIGGER_DISTANCE * metrics.density);
            }
        }
    }

    @Override
    public void draw(Canvas canvas) {
        super.draw(canvas);
        mProgressBar.draw(canvas);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        final int width = getMeasuredWidth();
        final int height = getMeasuredHeight();
        mProgressBar.setBounds(0, 0, width, mProgressBarHeight);
        if (getChildCount() == 0) {
            return;
        }
        final View child = getChildAt(0);
        final int childLeft = getPaddingLeft();
        final int childTop = mCurrentTargetOffsetTop + getPaddingTop();
        final int childWidth = width - getPaddingLeft() - getPaddingRight();
        final int childHeight = height - getPaddingTop() - getPaddingBottom();
        child.layout(childLeft, childTop, childLeft + childWidth, childTop + childHeight);
    }

    @Override
    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        if (getChildCount() > 1 && !isInEditMode()) {
            throw new IllegalStateException("SwipeRefreshLayout can host only one direct child");
        }
        if (getChildCount() > 0) {
            // super???view
            getChildAt(0).measure(
                    MeasureSpec.makeMeasureSpec(getMeasuredWidth() - getPaddingLeft() - getPaddingRight(),
                            MeasureSpec.EXACTLY),
                    MeasureSpec.makeMeasureSpec(getMeasuredHeight() - getPaddingTop() - getPaddingBottom(),
                            MeasureSpec.EXACTLY));
        }
    }

    // ?view????
    public boolean canChildScrollUp() {
        if (android.os.Build.VERSION.SDK_INT < 14) {
            if (mTarget instanceof AbsListView) {
                final AbsListView absListView = (AbsListView) mTarget;
                Logger.e("absListView.getChildAt(0).getTop()=" + absListView.getChildAt(0).getTop());
                return absListView.getChildCount() > 0 && (absListView.getFirstVisiblePosition() > 0
                        || absListView.getChildAt(0).getTop() < absListView.getPaddingTop());
            } else {
                return mTarget.getScrollY() > 0;
            }
        } else {
            return ViewCompat.canScrollVertically(mTarget, -1);
        }
    }

    private float mLastMotionY;// ?y???
    private float mInitialMotionY;// downy???
    private int mActivePointerId = INVALID_POINTER;// Action_DOWND

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        ensureTarget();
        final int action = MotionEventCompat.getActionMasked(ev);// 
        if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
            // ????
            mReturningToStart = false;
        }
        if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
            // viewGroup????view???
            // ?view
            return false;
        }
        // 
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionY = mInitialMotionY = ev.getY();
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);// Action_DOWND
            mIsBeingDragged = false;
            mCurrPercentage = 0;
            break;
        // ----------------------------------------------------
        case MotionEvent.ACTION_MOVE:
            if (mActivePointerId == INVALID_POINTER) {
                // 
                Logger.e("Got ACTION_MOVE event but don't have an active pointer id.");
                return false;
            }
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex < 0) {
                // ???
                Logger.e("Got ACTION_MOVE event but have an invalid active pointer id.");
                return false;
            }
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = y - mInitialMotionY;// y?
            if (yDiff > mTouchSlop) {// 
                mLastMotionY = y;
                mIsBeingDragged = true;
            }
            break;
        case MotionEventCompat.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);// ?
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mCurrPercentage = 0;
            mActivePointerId = INVALID_POINTER;
            break;
        }
        // true?onTouchEventmoveup?
        // false?view?moveup?
        return mIsBeingDragged;
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);// ?
        if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
            // ???
            mReturningToStart = false;
        }
        if (!isEnabled() || mReturningToStart || canChildScrollUp()) {
            // viewGroup????view??
            // view
            return false;
        }
        switch (action) {
        case MotionEvent.ACTION_DOWN:
            mLastMotionY = mInitialMotionY = ev.getY();
            mActivePointerId = MotionEventCompat.getPointerId(ev, 0);
            mIsBeingDragged = false;
            mCurrPercentage = 0;
            break;
        case MotionEvent.ACTION_MOVE:
            final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
            if (pointerIndex < 0) {
                // ???
                Logger.e("Got ACTION_MOVE event but have an invalid active pointer id.");
                return false;
            }
            final float y = MotionEventCompat.getY(ev, pointerIndex);
            final float yDiff = y - mInitialMotionY;// down??
            if (!mIsBeingDragged && yDiff > mTouchSlop) {
                mIsBeingDragged = true;
            }
            if (mIsBeingDragged) {
                if (yDiff > mDistanceToTriggerSync) {
                    // ????starRefresh
                    startRefresh();
                } else {
                    // ????
                    // ?
                    setTriggerPercentage(mAccelerateInterpolator.getInterpolation(yDiff / mDistanceToTriggerSync));
                    updateContentOffsetTop((int) (yDiff));// view??
                    if (mLastMotionY > y && mTarget.getTop() == getPaddingTop()) {
                        // ?????
                        removeCallbacks(mCancel);
                    } else {
                        updatePositionTimeout();
                    }
                }
                mLastMotionY = y;
            }
            break;
        case MotionEventCompat.ACTION_POINTER_DOWN: {// ????
            final int index = MotionEventCompat.getActionIndex(ev);
            mLastMotionY = MotionEventCompat.getY(ev, index);
            mActivePointerId = MotionEventCompat.getPointerId(ev, index);
            break;
        }
        case MotionEventCompat.ACTION_POINTER_UP:
            onSecondaryPointerUp(ev);// ?
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            mIsBeingDragged = false;
            mActivePointerId = INVALID_POINTER;
            return false;
        }
        return true;
    }

    // ?
    private void onSecondaryPointerUp(MotionEvent ev) {
        // ?ACTION_UP?
        final int pointerIndex = MotionEventCompat.getActionIndex(ev);// ????
        final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);// ????
        if (pointerId == mActivePointerId) {
            // ==ACTION_DOWN?
            final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
            mLastMotionY = MotionEventCompat.getY(ev, newPointerIndex);
            mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
        }
    }

    // ?
    private void setTriggerPercentage(float percent) {
        if (percent == 0f) {
            mCurrPercentage = 0;
            return;
        }
        mCurrPercentage = percent;
        mProgressBar.setTriggerPercentage(percent);
    }

    // ?View?
    private void updateContentOffsetTop(int targetTop) {
        // targetTopdown??
        final int currentTop = mTarget.getTop();// ?view??
        if (targetTop > mDistanceToTriggerSync) {
            // ??
            targetTop = (int) mDistanceToTriggerSync;
        } else if (targetTop < 0) {
            // 0
            targetTop = 0;
        }
        setTargetOffsetTopAndBottom(targetTop - currentTop);
    }

    // ?viewy?
    private void setTargetOffsetTopAndBottom(int offset) {
        // offset?view?
        mTarget.offsetTopAndBottom(offset);
        mCurrentTargetOffsetTop = mTarget.getTop();
    }

    // ????viewrunnable?
    private void startRefresh() {
        removeCallbacks(mCancel);// ??
        mReturnToStartPosition.run();// ?view
        setRefreshing(true);// ?
        mListener.onRefresh();// 
    }

    // ??
    public void setRefreshing(boolean refreshing) {
        if (mRefreshing != refreshing) {
            ensureTarget();// ?view?
            mCurrPercentage = 0;
            mRefreshing = refreshing;
            if (mRefreshing) {
                mProgressBar.start();
            } else {
                mProgressBar.stop();
            }
        }
    }

    // ?????
    private void updatePositionTimeout() {
        removeCallbacks(mCancel);
        postDelayed(mCancel, RETURN_TO_ORIGINAL_POSITION_TIMEOUT);
    }

    public boolean isRefreshing() {
        return mRefreshing;
    }

    @Override
    public void requestDisallowInterceptTouchEvent(boolean b) {
        // ?viewGroup
        // Nope.
    }

    public void setOnRefreshListener(OnRefreshListener listener) {
        mListener = listener;
    }

    // ?
    public interface OnRefreshListener {

        public void onRefresh();
    }

    // ?
    private class BaseAnimationListener implements AnimationListener {

        @Override
        public void onAnimationStart(Animation animation) {
        }

        @Override
        public void onAnimationEnd(Animation animation) {
        }

        @Override
        public void onAnimationRepeat(Animation animation) {
        }
    }
}