public void getCategories() {
    layout.setRefreshing(true);
    level = String.valueOf(((ReadingChallengeApplication) this.getApplication()).getLevel());
    // get categories from api
    apiService.listCategoriesByLevel(
        level,
        new Callback<List<Category>>() {
          @Override
          public void success(List<Category> categories, Response response) {
            displayCategories(categories);
            layout.setRefreshing(false);
          }

          @Override
          public void failure(RetrofitError error) {
            displayAlertDialog(getString(R.string.error), getString(R.string.activity_home_error));
            layout.setRefreshing(false);
            Timber.e("Error get categories : " + error.getMessage());
          }
        });
  }
 public void setRefreshStyle(int type) {
   setRefreshing(false);
   switch (type) {
     case STYLE_MATERIAL:
       mRefreshDrawable = new MaterialDrawable(getContext(), this);
       break;
     case STYLE_CIRCLES:
       mRefreshDrawable = new CirclesDrawable(getContext(), this);
       break;
     case STYLE_WATER_DROP:
       mRefreshDrawable = new WaterDropDrawable(getContext(), this);
       break;
     case STYLE_RING:
       mRefreshDrawable = new RingDrawable(getContext(), this);
       break;
     default:
       throw new InvalidParameterException("Type does not exist");
   }
   mRefreshDrawable.setColorSchemeColors(mColorSchemeColors);
   mRefreshView.setImageDrawable(mRefreshDrawable);
 }
  @Override
  public boolean onTouchEvent(MotionEvent ev) {

    if (!mIsBeingDragged) {
      return super.onTouchEvent(ev);
    }

    final int action = MotionEventCompat.getActionMasked(ev);

    switch (action) {
      case MotionEvent.ACTION_MOVE:
        {
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          if (pointerIndex < 0) {
            return false;
          }

          final float y = MotionEventCompat.getY(ev, pointerIndex);
          //                final float yDiff = Math.abs(y - mInitialMotionY);
          final float yDiff = y - mInitialMotionY;
          final float scrollTop = yDiff * DRAG_RATE;
          float originalDragPercent = scrollTop / mTotalDragDistance;
          if (originalDragPercent < 0) {
            return false;
          }
          float dragPercent = Math.min(1f, Math.abs(originalDragPercent));
          //                float adjustedPercent = (float) Math.max(dragPercent - .4, 0) * 5 / 3;
          //                    float adjustedPercent = dragPercent;
          float extraOS = Math.abs(scrollTop) - mTotalDragDistance;
          float slingshotDist = mSpinnerFinalOffset;
          float tensionSlingshotPercent =
              Math.max(0, Math.min(extraOS, slingshotDist * 2) / slingshotDist);
          float tensionPercent =
              (float) ((tensionSlingshotPercent / 4) - Math.pow((tensionSlingshotPercent / 4), 2))
                  * 2f;
          float extraMove = (slingshotDist) * tensionPercent * 2;
          int targetY = (int) ((slingshotDist * dragPercent) + extraMove);
          if (mRefreshView.getVisibility() != View.VISIBLE) {
            mRefreshView.setVisibility(View.VISIBLE);
          }
          if (scrollTop < mTotalDragDistance) {
            mRefreshDrawable.setPercent(dragPercent);
          }
          setTargetOffsetTop(targetY - mCurrentOffsetTop, true);
          break;
        }
      case MotionEventCompat.ACTION_POINTER_DOWN:
        final int index = MotionEventCompat.getActionIndex(ev);
        mActivePointerId = MotionEventCompat.getPointerId(ev, index);
        break;
      case MotionEventCompat.ACTION_POINTER_UP:
        onSecondaryPointerUp(ev);
        break;
      case MotionEvent.ACTION_UP:
      case MotionEvent.ACTION_CANCEL:
        {
          if (mActivePointerId == INVALID_POINTER) {
            return false;
          }
          final int pointerIndex = MotionEventCompat.findPointerIndex(ev, mActivePointerId);
          final float y = MotionEventCompat.getY(ev, pointerIndex);
          final float overscrollTop = (y - mInitialMotionY) * DRAG_RATE;
          mIsBeingDragged = false;
          if (overscrollTop > mTotalDragDistance) {
            setRefreshing(true, true);
          } else {
            mRefreshing = false;
            animateOffsetToStartPosition();
          }
          mActivePointerId = INVALID_POINTER;
          return false;
        }
    }

    return true;
  }
 public void setRefreshing(boolean refreshing) {
   if (mRefreshing != refreshing) {
     setRefreshing(refreshing, false /* notify */);
   }
 }
 public void setRefreshDrawable(RefreshDrawable drawable) {
   setRefreshing(false);
   mRefreshDrawable = drawable;
   mRefreshDrawable.setColorSchemeColors(mColorSchemeColors);
   mRefreshView.setImageDrawable(mRefreshDrawable);
 }