Skip to content

Commit

Permalink
Merge pull request #25 from ShawnLin013/feature/Add-setWrapSelectorWh…
Browse files Browse the repository at this point in the history
…eel-in-xml

Feature/add set wrap selector wheel in xml
  • Loading branch information
ShawnLin013 authored Dec 9, 2016
2 parents f818626 + a0b021e commit 7c7b98f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 57 deletions.
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_textColor|The text color of the numbers.|
|np_textSize|The text size of the numbers.|
|np_typeface|The typeface of the numbers.|
|np_wheel_item_count|The number of items show in the selector wheel.|
|np_wheelItemCount|The number of items show in the selector wheel.|
|np_wrapSelectorWheel|Flag whether the selector should wrap around.|

## Gradle

Expand All @@ -96,7 +97,7 @@ buildscript {
}
dependencies {
compile 'com.shawnlin:number-picker:2.2.0'
compile 'com.shawnlin:number-picker:2.3.0'
}
```

Expand Down
4 changes: 2 additions & 2 deletions gradle.properties
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
VERSION_CODE=6
VERSION_NAME=2.2.0
VERSION_CODE=7
VERSION_NAME=2.3.0

GROUP=com.shawnlin
ARTIFACT_ID=number-picker
Expand Down
105 changes: 53 additions & 52 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -584,8 +584,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, mTextSize);
mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL);
mFormatter = stringToFormatter(attributesArray.getString(R.styleable.NumberPicker_np_formatter));
mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheel_item_count, mWheelItemCount);
attributesArray.recycle();
mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheelItemCount, mWheelItemCount);

// By default Linearlayout that we extend is not drawn. This is
// its draw() method is not called but dispatchDraw() is called
Expand Down Expand Up @@ -628,6 +627,9 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {

setWheelItemCount(mWheelItemCount);

mWrapSelectorWheel = attributesArray.getBoolean(R.styleable.NumberPicker_np_wrapSelectorWheel, mWrapSelectorWheel);
setWrapSelectorWheel(mWrapSelectorWheel);

if (mWidth != SIZE_UNSPECIFIED && mHeight != SIZE_UNSPECIFIED) {
setScaleX(mWidth / mMinWidth);
setScaleY(mHeight / mMaxHeight);
Expand Down Expand Up @@ -655,6 +657,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
setImportantForAccessibility(IMPORTANT_FOR_ACCESSIBILITY_YES);
}
}

attributesArray.recycle();
}

@Override
Expand Down Expand Up @@ -858,8 +862,6 @@ public boolean onTouchEvent(MotionEvent event) {
}
onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
}
mVelocityTracker.recycle();
mVelocityTracker = null;
} else {
int initialVelocity = (int) velocityTracker.getYVelocity();
if (Math.abs(initialVelocity) > mMinimumFlingVelocity) {
Expand All @@ -880,9 +882,9 @@ public boolean onTouchEvent(MotionEvent event) {
}
onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
}
mVelocityTracker.recycle();
mVelocityTracker = null;
}
mVelocityTracker.recycle();
mVelocityTracker = null;
}
break;
}
Expand Down Expand Up @@ -1040,51 +1042,51 @@ public void setEnabled(boolean enabled) {
@Override
public void scrollBy(int x, int y) {
int[] selectorIndices = mSelectorIndices;
if (!mWrapSelectorWheel && y > 0
int gap;
if (isHorizontalMode()) {
if (!mWrapSelectorWheel && x > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && y < 0
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (!mWrapSelectorWheel && x < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
if (isHorizontalMode()) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}

mCurrentScrollOffset += x;
while (mCurrentScrollOffset - mInitialScrollOffset > mSelectorTextGapWidth) {
mCurrentScrollOffset -= mSelectorElementSize;
decrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
gap = mSelectorTextGapWidth;
} else {
if (!mWrapSelectorWheel && y > 0
&& selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
while (mCurrentScrollOffset - mInitialScrollOffset < -mSelectorTextGapWidth) {
mCurrentScrollOffset += mSelectorElementSize;
incrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
if (!mWrapSelectorWheel && y < 0
&& selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
return;
}
} else {

mCurrentScrollOffset += y;
while (mCurrentScrollOffset - mInitialScrollOffset > mSelectorTextGapHeight) {
mCurrentScrollOffset -= mSelectorElementSize;
decrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
gap = mSelectorTextGapHeight;
}

while (mCurrentScrollOffset - mInitialScrollOffset > gap) {
mCurrentScrollOffset -= mSelectorElementSize;
decrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] <= mMinValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
while (mCurrentScrollOffset - mInitialScrollOffset < -mSelectorTextGapHeight) {
mCurrentScrollOffset += mSelectorElementSize;
incrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
}
while (mCurrentScrollOffset - mInitialScrollOffset < -gap) {
mCurrentScrollOffset += mSelectorElementSize;
incrementSelectorIndices(selectorIndices);
setValueInternal(selectorIndices[mWheelMiddleItemIndex], true);
if (!mWrapSelectorWheel && selectorIndices[mWheelMiddleItemIndex] >= mMaxValue) {
mCurrentScrollOffset = mInitialScrollOffset;
}
}
}
Expand Down Expand Up @@ -1637,17 +1639,16 @@ private void initializeSelectorWheel() {
initializeSelectorWheelIndices();
int[] selectorIndices = mSelectorIndices;
int totalTextSize = selectorIndices.length * (int) mTextSize;
float textGapCount = selectorIndices.length;
int editTextTextPosition;
if (isHorizontalMode()) {
float totalTextGapWidth = (getRight() - getLeft()) - totalTextSize;
float textGapCount = selectorIndices.length;
mSelectorTextGapWidth = (int) (totalTextGapWidth / textGapCount + 0.5f);
mSelectorElementSize = (int) mTextSize + mSelectorTextGapWidth;
// Ensure that the middle item is positioned the same as the text in mInputText
editTextTextPosition = mInputText.getRight() / 2;
} else {
float totalTextGapHeight = (getBottom() - getTop()) - totalTextSize;
float textGapCount = selectorIndices.length;
mSelectorTextGapHeight = (int) (totalTextGapHeight / textGapCount + 0.5f);
mSelectorElementSize = (int) mTextSize + mSelectorTextGapHeight;
// Ensure that the middle item is positioned the same as the text in mInputText
Expand Down Expand Up @@ -1677,10 +1678,8 @@ private void onScrollerFinished(Scroller scroller) {
updateInputTextView();
}
onScrollStateChange(OnScrollListener.SCROLL_STATE_IDLE);
} else {
if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
updateInputTextView();
}
} else if (mScrollState != OnScrollListener.SCROLL_STATE_TOUCH_SCROLL) {
updateInputTextView();
}
}

Expand All @@ -1698,7 +1697,7 @@ private void onScrollStateChange(int scrollState) {
}

/**
* Flings the selector with the given <code>velocityY</code>.
* Flings the selector with the given <code>velocity</code>.
*/
private void fling(int velocity) {
if (isHorizontalMode()) {
Expand Down Expand Up @@ -1919,7 +1918,9 @@ private void postSetSelectionCommand(int selectionStart, int selectionEnd) {
'\u0665', '\u0666', '\u0667', '\u0668', '\u0669',
// Extended Arabic-Indic
'\u06f0', '\u06f1', '\u06f2', '\u06f3', '\u06f4',
'\u06f5', '\u06f6', '\u06f7', '\u06f8', '\u06f9'
'\u06f5', '\u06f6', '\u06f7', '\u06f8', '\u06f9',
// Negative
'-'
};

/**
Expand Down
3 changes: 2 additions & 1 deletion library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
<attr name="np_textColor" format="color" />
<attr name="np_textSize" format="dimension" />
<attr name="np_typeface" format="string" />
<attr name="np_wheel_item_count" format="integer" />
<attr name="np_wheelItemCount" format="integer" />
<attr name="np_wrapSelectorWheel" format="boolean" />
</declare-styleable>
</resources>

0 comments on commit 7c7b98f

Please sign in to comment.