@SuppressWarnings("deprecation")
  private void init(Context context, AttributeSet attrs) {
    setOrientation(LinearLayout.VERTICAL);

    ViewConfiguration config = ViewConfiguration.get(context);
    mTouchSlop = config.getScaledTouchSlop();

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

    if (a.hasValue(R.styleable.PullToRefresh_ptrMode)) {
      mMode = Mode.mapIntToMode(a.getInteger(R.styleable.PullToRefresh_ptrMode, 0));
    }

    // Refreshable View
    // By passing the attrs, we can add ListView/GridView params via XML
    mRefreshableView = createRefreshableView(context, attrs);
    addRefreshableView(context, mRefreshableView);

    // We need to create now layouts now
    mHeaderLayout = createLoadingLayout(context, Mode.PULL_DOWN_TO_REFRESH, a);
    mFooterLayout = createLoadingLayout(context, Mode.PULL_UP_TO_REFRESH, a);

    // Styleables from XML
    if (a.hasValue(R.styleable.PullToRefresh_ptrHeaderBackground)) {
      Drawable background = a.getDrawable(R.styleable.PullToRefresh_ptrHeaderBackground);
      if (null != background) {
        setBackgroundDrawable(background);
      }
    }
    if (a.hasValue(R.styleable.PullToRefresh_ptrAdapterViewBackground)) {
      Drawable background = a.getDrawable(R.styleable.PullToRefresh_ptrAdapterViewBackground);
      if (null != background) {
        mRefreshableView.setBackgroundDrawable(background);
      }
    }
    if (a.hasValue(R.styleable.PullToRefresh_ptrOverScroll)) {
      mOverScrollEnabled = a.getBoolean(R.styleable.PullToRefresh_ptrOverScroll, true);
    }

    // Let the derivative classes have a go at handling attributes, then
    // recycle them...
    handleStyledAttributes(a);
    a.recycle();

    // Finally update the UI for the modes
    updateUIForMode();
  }
コード例 #2
0
  @SuppressWarnings("deprecation")
  private void init(Context context, AttributeSet attrs) {
    switch (getPullToRefreshScrollDirection()) {
      case HORIZONTAL:
        setOrientation(LinearLayout.HORIZONTAL);
        break;
      case VERTICAL:
      default:
        setOrientation(LinearLayout.VERTICAL);
        break;
    }

    setGravity(Gravity.CENTER);

    ViewConfiguration config = ViewConfiguration.get(context);
    mTouchSlop = config.getScaledTouchSlop();

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

    if (a.hasValue(R.styleable.PullToRefresh_ptrMode)) {
      mMode = Mode.mapIntToValue(a.getInteger(R.styleable.PullToRefresh_ptrMode, 0));
    }

    if (a.hasValue(R.styleable.PullToRefresh_ptrAnimationStyle)) {
      mLoadingAnimationStyle =
          AnimationStyle.mapIntToValue(
              a.getInteger(R.styleable.PullToRefresh_ptrAnimationStyle, 0));
    }

    // Refreshable View
    // By passing the attrs, we can add ListView/GridView params via XML
    mRefreshableView = createRefreshableView(context, attrs);
    addRefreshableView(context, mRefreshableView);

    // We need to create now layouts now
    mHeaderLayout = createLoadingLayout(context, Mode.PULL_FROM_START, a);
    mFooterLayout = createLoadingLayout(context, Mode.PULL_FROM_END, a);

    /** Styleables from XML */
    if (a.hasValue(R.styleable.PullToRefresh_ptrRefreshableViewBackground)) {
      Drawable background = a.getDrawable(R.styleable.PullToRefresh_ptrRefreshableViewBackground);
      if (null != background) {
        mRefreshableView.setBackgroundDrawable(background);
      }
    } else if (a.hasValue(R.styleable.PullToRefresh_ptrAdapterViewBackground)) {
      Utils.warnDeprecation("ptrAdapterViewBackground", "ptrRefreshableViewBackground");
      Drawable background = a.getDrawable(R.styleable.PullToRefresh_ptrAdapterViewBackground);
      if (null != background) {
        mRefreshableView.setBackgroundDrawable(background);
      }
    }

    if (a.hasValue(R.styleable.PullToRefresh_ptrOverScroll)) {
      mOverScrollEnabled = a.getBoolean(R.styleable.PullToRefresh_ptrOverScroll, true);
    }

    if (a.hasValue(R.styleable.PullToRefresh_ptrScrollingWhileRefreshingEnabled)) {
      mScrollingWhileRefreshingEnabled =
          a.getBoolean(R.styleable.PullToRefresh_ptrScrollingWhileRefreshingEnabled, false);
    }

    // Let the derivative classes have a go at handling attributes, then
    // recycle them...
    handleStyledAttributes(a);
    a.recycle();

    // Finally update the UI for the modes
    updateUIForMode();
  }