Skip to content

Commit

Permalink
Merge pull request #90 from ShawnLin013/feature/Disable-scroller
Browse files Browse the repository at this point in the history
Feature/disable scroller
  • Loading branch information
ShawnLin013 authored Jan 26, 2018
2 parents b1daa87 + e5b46e8 commit 7f5bc98
Show file tree
Hide file tree
Showing 5 changed files with 67 additions and 6 deletions.
11 changes: 11 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ numberPicker.setMaxValue(data.length);
numberPicker.setDisplayedValues(data);
numberPicker.setValue(7);

// Set fading edge enabled
numberPicker.setFadingEdgeEnabled(true);

// Set scroller enabled
numberPicker.setScrollerEnabled(true);

// Set wrap selector wheel
numberPicker.setWrapSelectorWheel(true);

// OnClickListener
numberPicker.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down Expand Up @@ -116,11 +125,13 @@ add `xmlns:app="http://schemas.android.com/apk/res-auto"`
|np_dividerColor|The color of the selection divider.|
|np_dividerDistance|The distance between the two selection dividers.|
|np_dividerThickness|The thickness of the selection divider.|
|np_fadingEdgeEnabled|Flag whether the fading edge should enabled.|
|np_formatter|The formatter of the numbers.|
|np_max|The max value of this widget.|
|np_min|The min value of this widget.|
|np_order|The order of this widget. Default is ascending.|
|np_orientation|The orientation of this widget. Default is vertical.|
|np_scrollerEnabled|Flag whether the scroller should enabled.|
|np_selectedTextColor|The text color of the selected number.|
|np_selectedTextSize|The text size of the selected number.|
|np_textColor|The text color of the numbers.|
Expand Down
48 changes: 43 additions & 5 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,16 @@ public static final Formatter getTwoDigitFormatter() {
*/
private int mOrder;

/**
* Flag whether the fading edge should enabled.
*/
private boolean mFadingEdgeEnabled = true;

/**
* Flag whether the scroller should enabled.
*/
private boolean mScrollerEnabled = true;

/**
* The context of this widget.
*/
Expand Down Expand Up @@ -615,6 +625,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mTextSize = attributesArray.getDimension(R.styleable.NumberPicker_np_textSize, spToPx(mTextSize));
mTypeface = Typeface.create(attributesArray.getString(R.styleable.NumberPicker_np_typeface), Typeface.NORMAL);
mFormatter = stringToFormatter(attributesArray.getString(R.styleable.NumberPicker_np_formatter));
mFadingEdgeEnabled = attributesArray.getBoolean(R.styleable.NumberPicker_np_fadingEdgeEnabled, mFadingEdgeEnabled);
mScrollerEnabled = attributesArray.getBoolean(R.styleable.NumberPicker_np_scrollerEnabled, mScrollerEnabled);
mWheelItemCount = attributesArray.getInt(R.styleable.NumberPicker_np_wheelItemCount, mWheelItemCount);

// By default Linearlayout that we extend is not drawn. This is
Expand Down Expand Up @@ -842,6 +854,9 @@ public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
if (!isScrollerEnabled()) {
return false;
}
if (mVelocityTracker == null) {
mVelocityTracker = VelocityTracker.obtain();
}
Expand Down Expand Up @@ -997,6 +1012,10 @@ public boolean dispatchTrackballEvent(MotionEvent event) {

@Override
public void computeScroll() {
if (!isScrollerEnabled()) {
return;
}

Scroller scroller = mFlingScroller;
if (scroller.isFinished()) {
scroller = mAdjustScroller;
Expand Down Expand Up @@ -1035,6 +1054,9 @@ public void setEnabled(boolean enabled) {

@Override
public void scrollBy(int x, int y) {
if (!isScrollerEnabled()) {
return;
}
int[] selectorIndices = getSelectorIndices();
int gap;
if (isHorizontalMode()) {
Expand Down Expand Up @@ -1409,22 +1431,22 @@ public void setDisplayedValues(String[] displayedValues) {

@Override
protected float getTopFadingEdgeStrength() {
return isHorizontalMode() ? 0: FADING_EDGE_STRENGTH;
return !isHorizontalMode() && mFadingEdgeEnabled ? FADING_EDGE_STRENGTH : 0;
}

@Override
protected float getBottomFadingEdgeStrength() {
return isHorizontalMode() ? 0: FADING_EDGE_STRENGTH;
return !isHorizontalMode() && mFadingEdgeEnabled ? FADING_EDGE_STRENGTH : 0;
}

@Override
protected float getLeftFadingEdgeStrength() {
return isHorizontalMode() ? FADING_EDGE_STRENGTH : 0;
return isHorizontalMode() && mFadingEdgeEnabled ? FADING_EDGE_STRENGTH : 0;
}

@Override
protected float getRightFadingEdgeStrength() {
return isHorizontalMode() ? FADING_EDGE_STRENGTH : 0;
return isHorizontalMode() && mFadingEdgeEnabled ? FADING_EDGE_STRENGTH : 0;
}

@Override
Expand Down Expand Up @@ -1507,7 +1529,7 @@ protected void onDraw(Canvas canvas) {
public void onInitializeAccessibilityEvent(AccessibilityEvent event) {
super.onInitializeAccessibilityEvent(event);
event.setClassName(NumberPicker.class.getName());
event.setScrollable(true);
event.setScrollable(isScrollerEnabled());
final int scroll = (mMinValue + mValue) * mSelectorElementSize;
final int maxScroll = (mMaxValue - mMinValue) * mSelectorElementSize;
if (isHorizontalMode()) {
Expand Down Expand Up @@ -2150,6 +2172,14 @@ public void setFormatter(@StringRes int stringId) {
setFormatter(getResources().getString(stringId));
}

public void setFadingEdgeEnabled(boolean fadingEdgeEnabled) {
mFadingEdgeEnabled = fadingEdgeEnabled;
}

public void setScrollerEnabled(boolean scrollerEnabled) {
mScrollerEnabled = scrollerEnabled;
}

public void setSelectedTextColor(@ColorInt int color) {
mSelectedTextColor = color;
mSelectedText.setTextColor(mSelectedTextColor);
Expand Down Expand Up @@ -2281,6 +2311,14 @@ public Formatter getFormatter() {
return mFormatter;
}

public boolean isFadingEdgeEnabled() {
return mFadingEdgeEnabled;
}

public boolean isScrollerEnabled() {
return mScrollerEnabled;
}

public int getSelectedTextColor() {
return mSelectedTextColor;
}
Expand Down
2 changes: 2 additions & 0 deletions library/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
<attr name="np_dividerDistance" format="dimension" />
<attr name="np_dividerThickness" format="dimension" />
<attr name="np_formatter" format="string" />
<attr name="np_fadingEdgeEnabled" format="boolean" />
<attr name="np_max" format="integer" />
<attr name="np_min" format="integer" />
<attr name="np_order" format="enum">
Expand All @@ -20,6 +21,7 @@
<enum name="horizontal" value="0" />
<enum name="vertical" value="1" />
</attr>
<attr name="np_scrollerEnabled" format="boolean" />
<attr name="np_selectedTextColor" format="color" />
<attr name="np_selectedTextSize" format="dimension" />
<attr name="np_textColor" format="color" />
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,15 @@ protected void onCreate(Bundle savedInstanceState) {
numberPicker.setMinValue(0);
numberPicker.setValue(3);

// Set fading edge enabled
numberPicker.setFadingEdgeEnabled(true);

// Set scroller enabled
numberPicker.setScrollerEnabled(true);

// Set wrap selector wheel
numberPicker.setWrapSelectorWheel(true);

// OnClickListener
numberPicker.setOnClickListener(new View.OnClickListener() {
@Override
Expand Down
3 changes: 2 additions & 1 deletion sample/src/main/res/layout/content_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@
app:np_order="descending"
app:np_orientation="horizontal"
app:np_selectedTextColor="@color/colorAccent"
app:np_selectedTextSize="@dimen/selected_text_size"
app:np_textColor="@color/colorAccent"
app:np_textSize="@dimen/text_size"
app:np_selectedTextSize="@dimen/selected_text_size"
app:np_typeface="@string/roboto_light"
app:np_fadingEdgeEnabled="false"
app:np_wrapSelectorWheel="true" />

</RelativeLayout>

0 comments on commit 7f5bc98

Please sign in to comment.