private void updateAdapterData() {
    mAdapter.clear();

    final int maxYear = mMaxDate.get(Calendar.YEAR);
    for (int year = mMinDate.get(Calendar.YEAR); year <= maxYear; year++) {
      mAdapter.add(year);
    }
  }
 @Override
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   mController.tryVibrate();
   if (position != mSelectedPosition) {
     mSelectedPosition = position;
     mAdapter.notifyDataSetChanged();
   }
   mController.onYearSelected(mAdapter.getItem(position));
 }
Example #3
0
  @Override
  public Parcelable onSaveInstanceState() {
    Parcelable superState = super.onSaveInstanceState();

    SavedState ss = new SavedState(superState);

    ss.yearMin = mAdapter.getMinYear();
    ss.yearMax = mAdapter.getMaxYear();
    ss.year = mAdapter.getYear();

    return ss;
  }
 @Override
 public void onDateChanged() {
   updateAdapterData();
   mAdapter.notifyDataSetChanged();
   postSetSelectionCentered(
       mController.getSelectedDay().get(Calendar.YEAR) - mMinDate.get(Calendar.YEAR));
 }
Example #5
0
 @Override
 protected void onSizeChanged(int w, int h, int oldw, int oldh) {
   float shift = (h / (float) mItemRealHeight - 1) / 2;
   mPositionShift = (int) Math.floor(shift);
   mPositionShift = shift > mPositionShift ? mPositionShift + 1 : mPositionShift;
   mDistanceShift = (int) ((shift - mPositionShift) * mItemRealHeight) - getPaddingTop();
   goTo(mAdapter.getYear());
 }
Example #6
0
 /**
  * Jump to a specific year.
  *
  * @param year
  */
 public void goTo(int year) {
   int position = mAdapter.positionOfYear(year) - mPositionShift;
   int offset = mDistanceShift;
   if (position < 0) {
     position = 0;
     offset = 0;
   }
   postSetSelectionFromTop(position, offset);
 }
Example #7
0
  @Override
  protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
    int heightMode = MeasureSpec.getMode(heightMeasureSpec);
    int heightSize = MeasureSpec.getSize(heightMeasureSpec);

    measureItemHeight();

    if (heightMode != MeasureSpec.EXACTLY) {
      if (heightMode == MeasureSpec.AT_MOST) {
        int num = Math.min(mAdapter.getCount(), heightSize / mItemRealHeight);
        if (num >= 3) heightSize = mItemRealHeight * (num % 2 == 0 ? num - 1 : num);
      } else heightSize = mItemRealHeight * mAdapter.getCount();
      heightMeasureSpec =
          MeasureSpec.makeMeasureSpec(
              heightSize + getPaddingTop() + getPaddingBottom(), MeasureSpec.EXACTLY);
    }

    super.onMeasure(widthMeasureSpec, heightMeasureSpec);
  }
 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
   mController.tryVibrate();
   TextViewWithCircularIndicator clickedView = (TextViewWithCircularIndicator) view;
   if (clickedView != null) {
     if (clickedView != mSelectedView) {
       if (mSelectedView != null) {
         mSelectedView.drawIndicator(false);
         mSelectedView.requestLayout();
       }
       clickedView.drawIndicator(true);
       clickedView.requestLayout();
       mSelectedView = clickedView;
     }
     mController.onYearSelected(getYearFromTextView(clickedView));
     mAdapter.notifyDataSetChanged();
   }
 }
 public void onDateChanged() {
   mAdapter.notifyDataSetChanged();
   postSetSelectionCentered(mController.getSelectedDay().year - mController.getMinYear());
 }
Example #10
0
 /** @return The selected year value. */
 public int getYear() {
   return mAdapter.getYear();
 }
Example #11
0
  /**
   * Set the selected year.
   *
   * @param year The selected year value.
   */
  public void setYear(int year) {
    if (mAdapter.getYear() == year) return;

    mAdapter.setYear(year);
    goTo(year);
  }
Example #12
0
 /**
  * Set the range of selectable year value.
  *
  * @param min The minimum selectable year value.
  * @param max The maximum selectable year value.
  */
 public void setYearRange(int min, int max) {
   mAdapter.setYearRange(min, max);
 }
Example #13
0
  @Override
  protected void applyStyle(
      Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
    super.applyStyle(context, attrs, defStyleAttr, defStyleRes);

    TypedArray a =
        context.obtainStyledAttributes(attrs, R.styleable.YearPicker, defStyleAttr, defStyleRes);

    int year = -1;
    int yearMin = -1;
    int yearMax = -1;
    String familyName = null;
    int style = -1;

    for (int i = 0, count = a.getIndexCount(); i < count; i++) {
      int attr = a.getIndex(i);

      if (attr == R.styleable.YearPicker_dp_yearTextSize)
        mTextSize = a.getDimensionPixelSize(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_year) year = a.getInteger(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_yearMin) yearMin = a.getInteger(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_yearMax) yearMax = a.getInteger(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_yearItemHeight)
        mItemHeight = a.getDimensionPixelSize(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_textColor) mTextColors[0] = a.getColor(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_textHighlightColor)
        mTextColors[1] = a.getColor(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_selectionColor)
        mSelectionColor = a.getColor(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_animDuration)
        mAnimDuration = a.getInteger(attr, 0);
      else if (attr == R.styleable.YearPicker_dp_inInterpolator)
        mInInterpolator = AnimationUtils.loadInterpolator(context, a.getResourceId(attr, 0));
      else if (attr == R.styleable.YearPicker_dp_outInterpolator)
        mOutInterpolator = AnimationUtils.loadInterpolator(context, a.getResourceId(attr, 0));
      else if (attr == R.styleable.YearPicker_dp_fontFamily) familyName = a.getString(attr);
      else if (attr == R.styleable.YearPicker_dp_textStyle) style = a.getInteger(attr, 0);
    }

    a.recycle();

    if (mTextSize < 0)
      mTextSize =
          context.getResources().getDimensionPixelOffset(R.dimen.abc_text_size_title_material);

    if (mItemHeight < 0) mItemHeight = ThemeUtil.dpToPx(context, 48);

    if (mAnimDuration < 0)
      mAnimDuration = context.getResources().getInteger(android.R.integer.config_mediumAnimTime);

    if (mInInterpolator == null) mInInterpolator = new DecelerateInterpolator();

    if (mOutInterpolator == null) mOutInterpolator = new DecelerateInterpolator();

    if (familyName != null || style >= 0) mTypeface = TypefaceUtil.load(context, familyName, style);

    if (yearMin >= 0 || yearMax >= 0) {
      if (yearMin < 0) yearMin = mAdapter.getMinYear();

      if (yearMax < 0) yearMax = mAdapter.getMaxYear();

      if (yearMax < yearMin) yearMax = Integer.MAX_VALUE;

      setYearRange(yearMin, yearMax);
    }

    if (mAdapter.getYear() < 0 && year < 0) {
      Calendar cal = Calendar.getInstance();
      year = cal.get(Calendar.YEAR);
    }

    if (year >= 0) {
      year = Math.max(yearMin, Math.min(yearMax, year));
      setYear(year);
    }

    mAdapter.notifyDataSetChanged();
    requestLayout();
  }