1. 程式人生 > >SwipeRefreshLayout完美新增及完善上拉載入功能

SwipeRefreshLayout完美新增及完善上拉載入功能

關於Google推出的下拉重新整理控制元件SwipeRefreshLayout的相關使用方法,大家可以去參考http://blog.csdn.net/geeklei/article/details/38876981,本文也借鑑了其中的一些內容和“顏路的部落格”中《官方下拉重新整理SwipeRefreshLayout增加上拉載入更多》一文。

話不多說,直接先上改造效果圖(截圖時卡,湊合看吧):

下拉重新整理和上拉載入

     

簡單講下原始程式碼的原理:

下拉時,計算手指移動距離,如果超過一個系統預設的臨界值mTouchSlop,該事件就不下發到子控制元件進行處理,而是SwipeRefreshLayout自己處理。

變數mDistanceToTriggerSync指定了下拉重新整理的臨界值,如果下拉距離沒有大於該值,則計算下拉距離和mDistanceToTriggerSync的比值,並用該值作為進度百分比對進度條mProgressBar進行設定,同時移動子控制元件(ListView之類)的位置,螢幕上可以看到進度條顏色緩慢拉長的動畫,同時子控制元件向下移動。

如果下拉距離大於mDistanceToTriggerSync,則設定動畫把子控制元件位置復位,然後啟動下拉重新整理的色條迴圈動畫,並執行下拉重新整理的監聽事件。

關於進度條SwipeProgressBar的動畫顯示,Google的程式碼裡埋藏了一個坑人的陷阱。現象就是如果你在底部加了進度條,動畫效果異常,不會出現漸變的色條,只是生硬的轉換。上面參考的文章裡也碰到了這個問題。其實原因很簡單,看下圖:

把進度條SwipeProgressBar的高度設定大了後,可以看出其動畫效果是在進度條的中心向外部迴圈畫圓,每個迴圈中圓的顏色不同。重點是圓心的位置。

看SwipeProgressBar的如下程式碼,會發現在計算圓心高度cy的時候,取值是進度條高度的一半,這樣的話圓心會一直在上面,底部進度條自然動畫異常

  1. void draw(Canvas canvas) {  
  2.     finalint width = mBounds.width();  
  3.     finalint height = mBounds.height();  
  4.     finalint cx = width / 2;  
  5.     finalint cy = height / 2;  
  6.     boolean drawTriggerWhileFinishing = false;  
  7.     int restoreCount = canvas.save();  
  8.     canvas.clipRect(mBounds);  

修改SwipeProgressBar的程式碼,使其圓心在所在進度條的中心:
  1. void draw(Canvas canvas) {  
  2.         finalint width = mBounds.width();  
  3.         finalint height = mBounds.height();  
  4.         finalint cx = width / 2;  
  5. //        final int cy = height / 2;
  6.         finalint cy = mBounds.bottom - height / 2;  
  7.         boolean drawTriggerWhileFinishing = false;  
  8.         int restoreCount = canvas.save();  
  9.         canvas.clipRect(mBounds);  

效果如圖:


明白了原始程式碼的原理,就好入手進行修改了,修改的程式碼會在後面貼出來,註釋很詳細,這裡就不具體分析了。對SDK<14的滑動部分暫時沒有進行處理,直接返回了false,待後續改進。已改進

下面看修改後的功能:

1.可設定是否開啟下拉重新整理功能,可設定是否開啟上拉載入功能,預設全部開啟。

2.可設定是否在資料不滿一屏的情況下開啟上拉載入功能,預設關閉。

3.可單獨設定上下進度條的顏色,也可同時設定一樣的顏色。

囉嗦了這麼多,上程式碼:

SwipeProgressBar:

  1. package com.dahuo.learn.swiperefreshandload.view;  
  2. /* 
  3.  * Copyright (C) 2013 The Android Open Source Project 
  4.  * 
  5.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  6.  * you may not use this file except in compliance with the License. 
  7.  * You may obtain a copy of the License at 
  8.  * 
  9.  *      http://www.apache.org/licenses/LICENSE-2.0 
  10.  * 
  11.  * Unless required by applicable law or agreed to in writing, software 
  12.  * distributed under the License is distributed on an "AS IS" BASIS, 
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  14.  * See the License for the specific language governing permissions and 
  15.  * limitations under the License. 
  16.  */
  17. import android.graphics.Canvas;  
  18. import android.graphics.Paint;  
  19. import android.graphics.Rect;  
  20. import android.graphics.RectF;  
  21. import android.support.v4.view.ViewCompat;  
  22. import android.view.View;  
  23. import android.view.animation.AnimationUtils;  
  24. import android.view.animation.Interpolator;  
  25. /** 
  26.  * Custom progress bar that shows a cycle of colors as widening circles that 
  27.  * overdraw each other. When finished, the bar is cleared from the inside out as 
  28.  * the main cycle continues. Before running, this can also indicate how close 
  29.  * the user is to triggering something (e.g. how far they need to pull down to 
  30.  * trigger a refresh). 
  31.  */
  32. finalclass SwipeProgressBar {  
  33.     // Default progress animation colors are grays.
  34.     privatefinalstaticint COLOR1 = 0xB3000000;  
  35.     privatefinalstaticint COLOR2 = 0x80000000;  
  36.     privatefinalstaticint COLOR3 = 0x4d000000;  
  37.     privatefinalstaticint COLOR4 = 0x1a000000;  
  38.     // The duration of the animation cycle.
  39.     privatestaticfinalint ANIMATION_DURATION_MS = 2000;  
  40.     // The duration of the animation to clear the bar.
  41.     privatestaticfinalint FINISH_ANIMATION_DURATION_MS = 1000;  
  42.     // Interpolator for varying the speed of the animation.
  43.     privatestaticfinal Interpolator INTERPOLATOR = BakedBezierInterpolator.getInstance();  
  44.     privatefinal Paint mPaint = new Paint();  
  45.     privatefinal RectF mClipRect = new RectF();  
  46.     privatefloat mTriggerPercentage;  
  47.     privatelong mStartTime;  
  48.     privatelong mFinishTime;  
  49.     privateboolean mRunning;  
  50.     // Colors used when rendering the animation,
  51.     privateint mColor1;  
  52.     privateint mColor2;  
  53.     privateint mColor3;  
  54.     privateint mColor4;  
  55.     private View mParent;  
  56.     private Rect mBounds = new Rect();  
  57.     public SwipeProgressBar(View parent) {  
  58.         mParent = parent;  
  59.         mColor1 = COLOR1;  
  60.         mColor2 = COLOR2;  
  61.         mColor3 = COLOR3;  
  62.         mColor4 = COLOR4;  
  63.     }  
  64.     /** 
  65.      * Set the four colors used in the progress animation. The first color will 
  66.      * also be the color of the bar that grows in response to a user swipe 
  67.      * gesture. 
  68.      * 
  69.      * @param color1 Integer representation of a color. 
  70.      * @param color2 Integer representation of a color. 
  71.      * @param color3 Integer representation of a color. 
  72.      * @param color4 Integer representation of a color. 
  73.      */
  74.     void setColorScheme(int color1, int color2, int color3, int color4) {  
  75.         mColor1 = color1;  
  76.         mColor2 = color2;  
  77.         mColor3 = color3;  
  78.         mColor4 = color4;  
  79.     }  
  80.     /** 
  81.      * Update the progress the user has made toward triggering the swipe 
  82.      * gesture. and use this value to update the percentage of the trigger that 
  83.      * is shown. 
  84.      */
  85.     void setTriggerPercentage(float triggerPercentage) {  
  86.         mTriggerPercentage = triggerPercentage;  
  87.         mStartTime = 0;  
  88.         ViewCompat.postInvalidateOnAnimation(mParent);  
  89.     }  
  90.     /** 
  91.      * Start showing the progress animation. 
  92.      */
  93.     void start() {  
  94.         if (!mRunning) {  
  95.             mTriggerPercentage = 0;  
  96.             mStartTime = AnimationUtils.currentAnimationTimeMillis();  
  97.             mRunning = true;  
  98.             mParent.postInvalidate();  
  99.         }  
  100.     }  
  101.     /** 
  102.      * Stop showing the progress animation. 
  103.      */
  104.     void stop() {  
  105.         if (mRunning) {  
  106.             mTriggerPercentage = 0;  
  107.             mFinishTime = AnimationUtils.currentAnimationTimeMillis();  
  108.             mRunning = false;  
  109.             mParent.postInvalidate();  
  110.         }  
  111.     }  
  112.     /** 
  113.      * @return Return whether the progress animation is currently running. 
  114.      */
  115.     boolean isRunning() {  
  116.         return mRunning || mFinishTime > 0;  
  117.     }  
  118.     void draw(Canvas canvas) {  
  119.         finalint width = mBounds.width();  
  120.         finalint height = mBounds.height();  
  121.         finalint cx = width / 2;  
  122. //        final int cy = height / 2;
  123.         finalint cy = mBounds.bottom - height / 2;  
  124.         boolean drawTriggerWhileFinishing = false;  
  125.         int restoreCount = canvas.save();  
  126.         canvas.clipRect(mBounds);  
  127.         if (mRunning || (mFinishTime > 0)) {  
  128.             long now = AnimationUtils.currentAnimationTimeMillis();  
  129.             long elapsed = (now - mStartTime) % ANIMATION_DURATION_MS;  
  130.             long iterations = (now - mStartTime) / ANIMATION_DURATION_MS;  
  131.             float rawProgress = (elapsed / (ANIMATION_DURATION_MS / 100f));  
  132.             // If we're not running anymore, that means we're running through
  133.             // the finish animation.
  134.             if (!mRunning) {  
  135.                 // If the finish animation is done, don't draw anything, and
  136.                 // don't repost.
  137.                 if ((now - mFinishTime) >= FINISH_ANIMATION_DURATION_MS) {  
  138.                     mFinishTime = 0;  
  139.                     return;  
  140.                 }  
  141.                 // Otherwise, use a 0 opacity alpha layer to clear the animation
  142.                 // from the inside out. This layer will prevent the circles from
  143.                 // drawing within its bounds.
  144.                 long finishElapsed = (now - mFinishTime) % FINISH_ANIMATION_DURATION_MS;  
  145.                 float finishProgress = (finishElapsed / (FINISH_ANIMATION_DURATION_MS / 100f));  
  146.                 float pct = (finishProgress / 100f);  
  147.                 // Radius of the circle is half of the screen.
  148.                 float clearRadius = width / 2 * INTERPOLATOR.getInterpolation(pct);  
  149.                 mClipRect.set(cx - clearRadius, 0, cx + clearRadius, height);  
  150.                 canvas.saveLayerAlpha(mClipRect, 00);  
  151.                 // Only draw the trigger if there is a space in the center of
  152.                 // this refreshing view that needs to be filled in by the
  153.                 // trigger. If the progress view is just still animating, let it
  154.                 // continue animating.
  155.                 drawTriggerWhileFinishing = true;  
  156.             }  
  157.             // First fill in with the last color that would have finished drawing.
  158.             if (iterations == 0) {  
  159.                 canvas.drawColor(mColor1);  
  160.             } else {  
  161.                 if (rawProgress >= 0 && rawProgress < 25) {  
  162.                     canvas.drawColor(mColor4);  
  163.                 } elseif (rawProgress >= 25 && rawProgress < 50) {  
  164.                     canvas.drawColor(mColor1);  
  165.                 } elseif (rawProgress >= 50 && rawProgress < 75) {  
  166.                     canvas.drawColor(mColor2);  
  167.                 } else {  
  168.                     canvas.drawColor(mColor3);  
  169.                 }  
  170.             }  
  171.             // Then draw up to 4 overlapping concentric circles of varying radii, based on how far
  172.             // along we are in the cycle.
  173.             // progress 0-50 draw mColor2
  174.             // progress 25-75 draw mColor3
  175.             // progress 50-100 draw mColor4
  176.             // progress 75 (wrap to 25) draw mColor1
  177.             if ((rawProgress >= 0 && rawProgress <= 25)) {  
  178.                 float pct = (((rawProgress + 25) * 2) / 100f);  
  179.                 drawCircle(canvas, cx, cy, mColor1, pct);  
  180.             }  
  181.             if (rawProgress >= 0 && rawProgress <= 50) {  
  182.                 float pct = ((rawProgress * 2) / 100f);  
  183.                 drawCircle(canvas, cx, cy, mColor2, pct);  
  184.             }  
  185.             if (rawProgress >= 25 && rawProgress <= 75) {  
  186.                 float pct = (((rawProgress - 25) * 2) / 100f);  
  187.                 drawCircle(canvas, cx, cy, mColor3, pct);  
  188.             }  
  189.             if (rawProgress >= 50 && rawProgress <= 100) {  
  190.                 float pct = (((rawProgress - 50) * 2) / 100f);  
  191.                 drawCircle(canvas, cx, cy, mColor4, pct);  
  192.             }  
  193.             if ((rawProgress >= 75 && rawProgress <= 100)) {  
  194.                 float pct = (((rawProgress - 75) * 2) / 100f);  
  195.                 drawCircle(canvas, cx, cy, mColor1, pct);  
  196.             }  
  197.             if (mTriggerPercentage > 0 && drawTriggerWhileFinishing) {  
  198.                 // There is some portion of trigger to draw. Restore the canvas,
  199.                 // then draw the trigger. Otherwise, the trigger does not appear
  200.                 // until after the bar has finished animating and appears to
  201.                 // just jump in at a larger width than expected.
  202.                 canvas.restoreToCount(restoreCount);  
  203.                 restoreCount = canvas.save();  
  204.                 canvas.clipRect(mBounds);  
  205.                 drawTrigger(canvas, cx, cy);  
  206.             }  
  207.             // Keep running until we finish out the last cycle.
  208.             ViewCompat.postInvalidateOnAnimation(mParent);  
  209.         } else {  
  210.             // Otherwise if we're in the middle of a trigger, draw that.
  211.             if (mTriggerPercentage > 0 && mTriggerPercentage <= 1.0) {  
  212.                 drawTrigger(canvas, cx, cy);  
  213.             }  
  214.         }  
  215.         canvas.restoreToCount(restoreCount);  
  216.     }  
  217.     privatevoid drawTrigger(Canvas canvas, int cx, int cy) {  
  218.         mPaint.setColor(mColor1);  
  219.         canvas.drawCircle(cx, cy, cx * mTriggerPercentage, mPaint);  
  220.     }  
  221.     /** 
  222.      * Draws a circle centered in the view. 
  223.      * 
  224.      * @param canvas the canvas to draw on 
  225.      * @param cx the center x coordinate 
  226.      * @param cy the center y coordinate 
  227.      * @param color the color to draw 
  228.      * @param pct the percentage of the view that the circle should cover 
  229.      */
  230.     privatevoid drawCircle(Canvas canvas, float cx, float cy, int color, float pct) {  
  231.         mPaint.setColor(color);  
  232.         canvas.save();  
  233.         canvas.translate(cx, cy);  
  234.         float radiusScale = INTERPOLATOR.getInterpolation(pct);  
  235.         canvas.scale(radiusScale, radiusScale);  
  236.         canvas.drawCircle(00, cx, mPaint);  
  237.         canvas.restore();  
  238.     }  
  239.     /** 
  240.      * Set the drawing bounds of this SwipeProgressBar. 
  241.      */
  242.     void setBounds(int left, int top, int right, int bottom) {  
  243.         mBounds.left = left;  
  244.         mBounds.top = top;  
  245.         mBounds.right = right;  
  246.         mBounds.bottom = bottom;  
  247.     }  
  248. }  

SwipeRefreshLayout:
  1. /* 
  2.  * Copyright (C) 2013 The Android Open Source Project 
  3.  * 
  4.  * Licensed under the Apache License, Version 2.0 (the "License"); 
  5.  * you may not use this file except in compliance with the License. 
  6.  * You may obtain a copy of the License at 
  7.  * 
  8.  *      http://www.apache.org/licenses/LICENSE-2.0 
  9.  * 
  10.  * Unless required by applicable law or agreed to in writing, software 
  11.  * distributed under the License is distributed on an "AS IS" BASIS, 
  12.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 
  13.  * See the License for the specific language governing permissions and 
  14.  * limitations under the License. 
  15.  */
  16. package com.dahuo.learn.swiperefreshandload.view;  
  17. import android.content.Context;  
  18. import android.content.res.Resources;  
  19. import android.content.res.TypedArray;  
  20. import android.graphics.Canvas;  
  21. import android.support.v4.view.MotionEventCompat;  
  22. import android.support.v4.view.ViewCompat;  
  23. import android.util.AttributeSet;  
  24. import android.util.DisplayMetrics;  
  25. import android.util.Log;  
  26. import android.view.MotionEvent;  
  27. import android.view.View;  
  28. import android.view.ViewConfiguration;  
  29. import android.view.ViewGroup;  
  30. import android.view.animation.AccelerateInterpolator;  
  31. import android.view.animation.Animation;  
  32. import android.view.animation.Animation.AnimationListener;  
  33. import android.view.animation.DecelerateInterpolator;  
  34. import android.view.animation.Transformation;  
  35. import android.widget.AbsListView;  
  36. /** 
  37.  * The SwipeRefreshLayout should be used whenever the user can refresh the 
  38.  * contents of a view via a vertical swipe gesture. The activity that 
  39.  * instantiates this view should add an OnRefreshListener to be notified 
  40.  * whenever the swipe to refresh gesture is completed. The SwipeRefreshLayout 
  41.  * will notify the listener each and every time the gesture is completed again; 
  42.  * the listener is responsible for correctly determining when to actually 
  43.  * initiate a refresh of its content. If the listener determines there should 
  44.  * not be a refresh, it must call setRefreshing(false) to cancel any visual 
  45.  * indication of a refresh. If an activity wishes to show just the progress 
  46.  * animation, it should call setRefreshing(true). To disable the gesture and progress 
  47.  * animation, call setEnabled(false) on the view. 
  48.  * 
  49.  * <p> This layout should be made the parent of the view that will be refreshed as a 
  50.  * result of the gesture and can only support one direct child. This view will 
  51.  * also be made the target of the gesture and will be forced to match both the 
  52.  * width and the height supplied in this layout. The SwipeRefreshLayout does not 
  53.  * provide accessibility events; instead, a menu item must be provided to allow 
  54.  * refresh of the content wherever this gesture is used.</p> 
  55.  */
  56. publicclass SwipeRefreshLayout extends ViewGroup {  
  57.     privatestaticfinal String LOG_TAG = SwipeRefreshLayout.class.getSimpleName();  
  58.     privatestaticfinallong RETURN_TO_ORIGINAL_POSITION_TIMEOUT = 300;  
  59.     privatestaticfinalfloat ACCELERATE_INTERPOLATION_FACTOR = 1.5f;  
  60.     privatestaticfinalfloat DECELERATE_INTERPOLATION_FACTOR = 2f;  
  61.     privatestaticfinalfloat PROGRESS_BAR_HEIGHT = 4;  
  62.     privatestaticfinalfloat MAX_SWIPE_DISTANCE_FACTOR = .6f;  
  63.     privatestaticfinalint REFRESH_TRIGGER_DISTANCE = 120;  
  64.     privatestaticfinalint INVALID_POINTER = -1;  
  65.     private SwipeProgressBar mProgressBar; //the thing that shows progress is going
  66.     private SwipeProgressBar mProgressBarBottom;  
  67.     private View mTarget; //the content that gets pulled down
  68.     privateint mOriginalOffsetTop;  
  69.     private OnRefreshListener mRefreshListener;  
  70.     private OnLoadListener mLoadListener;  
  71.     privateint mFrom;  
  72.     privateboolean mRefreshing = false;  
  73.     privateboolean mLoading = false;  
  74.     privateint mTouchSlop;  
  75.     privatefloat mDistanceToTriggerSync = -1;  
  76.     privateint mMediumAnimationDuration;  
  77.     privatefloat mFromPercentage = 0;  
  78.     privatefloat mCurrPercentage = 0;  
  79.     privateint mProgressBarHeight;  
  80.     privateint mCurrentTargetOffsetTop;  
  81.     privatefloat mInitialMotionY;  
  82.     privatefloat mLastMotionY;  
  83.     privateboolean mIsBeingDragged;  
  84.     privateint mActivePointerId = INVALID_POINTER;  
  85.     // Target is returning to its start offset because it was cancelled or a
  86.     // refresh was triggered.
  87.     privateboolean mReturningToStart;  
  88.     privatefinal DecelerateInterpolator mDecelerateInterpolator;  
  89.     privatefinal AccelerateInterpolator mAccelerateInterpolator;  
  90.     privatestaticfinalint[] LAYOUT_ATTRS = newint[] {  
  91.         android.R.attr.enabled  
  92.     };  
  93.     private Mode mMode = Mode.getDefault();  
  94.     //之前手勢的方向,為了解決同一個觸點前後移動方向不同導致後一個方向會重新整理的問題,
  95.     //這裡Mode.DISABLED無意義,只是一個初始值,和上拉/下拉方向進行區分
  96.     private Mode mLastDirection = Mode.DISABLED;  
  97.     privateint mDirection = 0;  
  98.     //當子控制元件移動到盡頭時才開始計算初始點的位置
  99.     privatefloat mStartPoint;  
  100.     privateboolean up;  
  101.     privateboolean down;  
  102.     //資料不足一屏時是否開啟上拉載入模式
  103.     privateboolean loadNoFull = false;  
  104.     //對下拉或上拉進行復位
  105.     privatefinal Animation mAnimateToStartPosition = new Animation() {  
  106.         @Override
  107.         publicvoid applyTransformation(float interpolatedTime, Transformation t) {  
  108.             int targetTop = 0;  
  109.             if (mFrom != mOriginalOffsetTop) {  
  110.                 targetTop = (mFrom + (int)((mOriginalOffsetTop - mFrom) * interpolatedTime));  
  111.             }  
  112.             int offset = targetTop - mTarget.getTop();  
  113.             //註釋掉這裡,不然上拉後回覆原位置會很快,不平滑
  114. //            final int currentTop = mTarget.getTop();
  115. //            if (offset + currentTop < 0) {
  116. //                offset = 0 - currentTop;
  117. //            }
  118.             setTargetOffsetTopAndBottom(offset);  
  119.         }  
  120.     };  
  121.     //設定上方進度條的完成度百分比
  122.     private Animation mShrinkTrigger = new Animation() {  
  123.         @Override
  124.         publicvoid applyTransformation(float interpolatedTime, Transformation t) {  
  125.             float percent = mFromPercentage + ((0 - mFromPercentage) * interpolatedTime);  
  126.             mProgressBar.setTriggerPercentage(percent);  
  127.         }  
  128.     };  
  129.     //設定下方進度條的完成度百分比
  130.     private Animation mShrinkTriggerBottom = new Animation() {  
  131.         @Override
  132.         publicvoid applyTransformation(float interpolatedTime, Transformation t) {  
  133.             float percent = mFromPercentage + ((0 - mFromPercentage) * interpolatedTime);  
  134.             mProgressBarBottom.setTriggerPercentage(percent);  
  135.         }  
  136.     };  
  137.     //監聽,回覆初始位置
  138.     privatefinal AnimationListener mReturnToStartPositionListener = new BaseAnimationListener() {  
  139.         @Override
  140.         publicvoid onAnimationEnd(Animation animation) {  
  141.             // Once the target content has returned to its start position, reset
  142.             // the target offset to 0
  143.             mCurrentTargetOffsetTop = 0;  
  144.             mLastDirection = Mode.DISABLED;  
  145.         }  
  146.     };  
  147.     //回覆進度條百分比
  148.     privatefinal AnimationListener mShrinkAnimationListener = new BaseAnimationListener() {  
  149.         @Override
  150.         publicvoid onAnimationEnd(Animation animation) {  
  151.             mCurrPercentage = 0;  
  152.         }  
  153.     };  
  154.     //回覆初始位置
  155.     privatefinal Runnable mReturnToStartPosition = new Runnable() {  
  156.         @Override
  157.         publicvoid run() {  
  158.             mReturningToStart = true;  
  159.             animateOffsetToStartPosition(mCurrentTargetOffsetTop + getPaddingTop(),  
  160.                     mReturnToStartPositionListener);  
  161.         }  
  162.     };  
  163.     // Cancel the refresh gesture and animate everything back to its original state.
  164.     privatefinal Runnable mCancel = new Runnable() {  
  165.         @Override
  166.         publicvoid run() {  
  167.             mReturningToStart = true;  
  168.             // Timeout fired since the user last moved their finger; animate the
  169.             // trigger to 0 and put the target back at its original position
  170.             if (mProgressBar != null || mProgressBarBottom != null) {  
  171.                 mFromPercentage = mCurrPercentage;  
  172.                 if(mDirection > 0 && ((mMode == Mode.PULL_FROM_START) || (mMode == Mode.BOTH)))  
  173.                 {  
  174.                     mShrinkTrigger.setDuration(mMediumAnimationDuration);  
  175.                     mShrinkTrigger.setAnimationListener(mShrinkAnimationListener);  
  176.                     mShrinkTrigger.reset();  
  177.                     mShrinkTrigger.setInterpolator(mDecelerateInterpolator);  
  178.                     startAnimation(mShrinkTrigger);  
  179.                 }  
  180.                 elseif(mDirection < 0 && ((mMode == Mode.PULL_FROM_END) || (mMode == Mode.BOTH)))  
  181.                 {  
  182.                     mShrinkTriggerBottom.setDuration(mMediumAnimationDuration);  
  183.                     mShrinkTriggerBottom.setAnimationListener(mShrinkAnimationListener);  
  184.                     mShrinkTriggerBottom.reset();  
  185.                     mShrinkTriggerBottom.setInterpolator(mDecelerateInterpolator);  
  186.                     startAnimation(mShrinkTriggerBottom);                     
  187.                 }  
  188.             }  
  189.             mDirection = 0;  
  190.             animateOffsetToStartPosition(mCurrentTargetOffsetTop + getPaddingTop(),  
  191.                     mReturnToStartPositionListener);  
  192.         }  
  193.     };  
  194.     /** 
  195.      * Simple constructor to use when creating a SwipeRefreshLayout from code. 
  196.      * @param context 
  197.      */
  198.     public SwipeRefreshLayout(Context context) {  
  199.         this(context, null);  
  200.     }  
  201.     /** 
  202.      * Constructor that is called when inflating SwipeRefreshLayout from XML. 
  203.      * @param context 
  204.      * @param attrs 
  205.      */
  206.     public SwipeRefreshLayout(Context context, AttributeSet attrs) {  
  207.         super(context, attrs);  
  208.         mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();  
  209.         mMediumAnimationDuration = getResources().getInteger(  
  210.                 android.R.integer.config_mediumAnimTime);