@Override
  protected final ListView createRefreshableView(Context context, AttributeSet attrs) {
    ListView lv = new InternalListView(context, attrs);

    // Get Styles from attrs
    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.PullToRefresh);

    // Create Loading Views ready for use later
    FrameLayout frame = new FrameLayout(context);
    mHeaderLoadingView = new LoadingLayout(context, Mode.PULL_DOWN_TO_REFRESH, a);
    frame.addView(
        mHeaderLoadingView,
        FrameLayout.LayoutParams.MATCH_PARENT,
        FrameLayout.LayoutParams.WRAP_CONTENT);
    mHeaderLoadingView.setVisibility(View.GONE);
    lv.addHeaderView(frame, null, false);

    mLvFooterLoadingFrame = new FrameLayout(context);
    mFooterLoadingView = new LoadingLayout(context, Mode.PULL_UP_TO_REFRESH, a);
    mLvFooterLoadingFrame.addView(
        mFooterLoadingView,
        FrameLayout.LayoutParams.MATCH_PARENT,
        FrameLayout.LayoutParams.WRAP_CONTENT);
    mFooterLoadingView.setVisibility(View.GONE);

    a.recycle();

    // Set it to this so it can be used in ListActivity/ListFragment
    lv.setId(android.R.id.list);
    return lv;
  }
  public void setReleaseLabel(String releaseLabel, Mode mode) {
    super.setReleaseLabel(releaseLabel, mode);

    if (null != mHeaderLoadingView && mode.canPullDown()) {
      mHeaderLoadingView.setReleaseLabel(releaseLabel);
    }
    if (null != mFooterLoadingView && mode.canPullUp()) {
      mFooterLoadingView.setReleaseLabel(releaseLabel);
    }
  }
  @Override
  protected void setRefreshingInternal(boolean doScroll) {

    // If we're not showing the Refreshing view, or the list is empty, then
    // the header/footer views won't show so we use the
    // normal method
    ListAdapter adapter = mRefreshableView.getAdapter();
    if (!getShowViewWhileRefreshing() || null == adapter || adapter.isEmpty()) {
      super.setRefreshingInternal(doScroll);
      return;
    }

    super.setRefreshingInternal(false);

    final LoadingLayout originalLoadingLayout, listViewLoadingLayout;
    final int selection, scrollToY;

    switch (getCurrentMode()) {
      case PULL_UP_TO_REFRESH:
        originalLoadingLayout = getFooterLayout();
        listViewLoadingLayout = mFooterLoadingView;
        selection = mRefreshableView.getCount() - 1;
        scrollToY = getScrollY() - getHeaderHeight();
        break;
      case PULL_DOWN_TO_REFRESH:
      default:
        originalLoadingLayout = getHeaderLayout();
        listViewLoadingLayout = mHeaderLoadingView;
        selection = 0;
        scrollToY = getScrollY() + getHeaderHeight();
        break;
    }

    if (doScroll) {
      // We scroll slightly so that the ListView's header/footer is at the
      // same Y position as our normal header/footer
      setHeaderScroll(scrollToY);
    }

    // Hide our original Loading View
    originalLoadingLayout.setVisibility(View.INVISIBLE);

    // Show the ListView Loading View and set it to refresh
    listViewLoadingLayout.setVisibility(View.VISIBLE);
    listViewLoadingLayout.refreshing();

    if (doScroll) {
      // Make sure the ListView is scrolled to show the loading
      // header/footer
      mRefreshableView.setSelection(selection);

      // Smooth scroll as normal
      smoothScrollTo(0);
    }
  }
  @Override
  protected void resetHeader() {

    // If we're not showing the Refreshing view, or the list is empty, then
    // the header/footer views won't show so we use the
    // normal method
    ListAdapter adapter = mRefreshableView.getAdapter();
    if (!getShowViewWhileRefreshing() || null == adapter || adapter.isEmpty()) {
      super.resetHeader();
      return;
    }

    LoadingLayout originalLoadingLayout;
    LoadingLayout listViewLoadingLayout;

    int scrollToHeight = getHeaderHeight();
    int selection;
    boolean scroll;

    switch (getCurrentMode()) {
      case PULL_UP_TO_REFRESH:
        originalLoadingLayout = getFooterLayout();
        listViewLoadingLayout = mFooterLoadingView;
        selection = mRefreshableView.getCount() - 1;
        scroll = mRefreshableView.getLastVisiblePosition() == selection;
        break;
      case PULL_DOWN_TO_REFRESH:
      default:
        originalLoadingLayout = getHeaderLayout();
        listViewLoadingLayout = mHeaderLoadingView;
        scrollToHeight *= -1;
        selection = 0;
        scroll = mRefreshableView.getFirstVisiblePosition() == selection;
        break;
    }

    // Set our Original View to Visible
    originalLoadingLayout.setVisibility(View.VISIBLE);

    /**
     * Scroll so the View is at the same Y as the ListView header/footer, but only scroll if we've
     * pulled to refresh and it's positioned correctly
     */
    if (scroll && getState() != MANUAL_REFRESHING) {
      mRefreshableView.setSelection(selection);
      setHeaderScroll(scrollToHeight);
    }

    // Hide the ListView Header/Footer
    listViewLoadingLayout.setVisibility(View.GONE);

    super.resetHeader();
  }