@Override
  protected void onRestoreInstanceState(Parcelable state) {
    if (state instanceof Bundle) {
      Bundle bundle = (Bundle) state;

      mMode = Mode.mapIntToMode(bundle.getInt(STATE_MODE, 0));
      mCurrentMode = Mode.mapIntToMode(bundle.getInt(STATE_CURRENT_MODE, 0));

      mDisableScrollingWhileRefreshing =
          bundle.getBoolean(STATE_DISABLE_SCROLLING_REFRESHING, true);
      mShowViewWhileRefreshing = bundle.getBoolean(STATE_SHOW_REFRESHING_VIEW, true);

      // Let super Restore Itself
      super.onRestoreInstanceState(bundle.getParcelable(STATE_SUPER));

      final int viewState = bundle.getInt(STATE_STATE, PULL_TO_REFRESH);
      if (viewState == REFRESHING) {
        setRefreshingInternal(true);
        mState = viewState;
      }
      return;
    }

    super.onRestoreInstanceState(state);
  }
 @Override
 public final void setRefreshing(boolean doScroll) {
   if (!isRefreshing()) {
     setRefreshingInternal(doScroll);
     mState = MANUAL_REFRESHING;
   }
 }
  @Override
  protected void setRefreshingInternal(boolean doScroll) {
    super.setRefreshingInternal(doScroll);

    if (getShowIndicatorInternal()) {
      updateIndicatorViewsVisibility();
    }
  }
  @Override
  public final boolean onTouchEvent(MotionEvent event) {

    if (!isPullToRefreshEnabled()) {
      return false;
    }

    // If we're refreshing, and the flag is set. Eat the event
    if (mDisableScrollingWhileRefreshing && isRefreshing()) {
      return true;
    }

    if (event.getAction() == MotionEvent.ACTION_DOWN && event.getEdgeFlags() != 0) {
      return false;
    }

    switch (event.getAction()) {
      case MotionEvent.ACTION_MOVE:
        {
          if (mIsBeingDragged) {
            mLastMotionY = event.getY();
            pullEvent();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_DOWN:
        {
          if (isReadyForPull()) {
            mLastMotionY = mInitialMotionY = event.getY();
            return true;
          }
          break;
        }

      case MotionEvent.ACTION_CANCEL:
      case MotionEvent.ACTION_UP:
        {
          if (mIsBeingDragged) {
            mIsBeingDragged = false;

            if (mState == RELEASE_TO_REFRESH) {
              if (null != mOnRefreshListener) {
                setRefreshingInternal(true);
                mOnRefreshListener.onRefresh(this);
                return true;

              } else if (null != mOnRefreshListener2) {
                setRefreshingInternal(true);
                if (mCurrentMode == Mode.PULL_DOWN_TO_REFRESH) {
                  mOnRefreshListener2.onPullDownToRefresh(this);
                } else if (mCurrentMode == Mode.PULL_UP_TO_REFRESH) {
                  mOnRefreshListener2.onPullUpToRefresh(this);
                }
                return true;
              } else {
                // If we don't have a listener, just reset
                resetHeader();
                return true;
              }
            }

            smoothScrollTo(0);
            return true;
          }
          break;
        }
    }

    return false;
  }