Skip to content

Commit

Permalink
Merge pull request #32 from ShawnLin013/feature/Fix-Horizontal-number…
Browse files Browse the repository at this point in the history
…-picker-scrolls-when-disabled

🐛 Fix horizontal number picker scrolls when disabled (#29)
  • Loading branch information
ShawnLin013 authored Jan 23, 2017
2 parents b13cd9d + 8eb7f25 commit afefd41
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 18 deletions.
43 changes: 27 additions & 16 deletions library/src/main/java/com/shawnlin/numberpicker/NumberPicker.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.support.annotation.DimenRes;
import android.support.annotation.IntDef;
import android.support.annotation.StringRes;
import android.support.v4.content.ContextCompat;
import android.text.InputFilter;
import android.text.InputType;
import android.text.Spanned;
Expand Down Expand Up @@ -461,6 +462,11 @@ public static final Formatter getTwoDigitFormatter() {
*/
private int mOrientation;

/**
* The context of this widget.
*/
private Context mContext;

/**
* Interface to listen for changes of the current value.
*/
Expand Down Expand Up @@ -550,9 +556,11 @@ public NumberPicker(Context context, AttributeSet attrs) {
*/
public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs);
mContext = context;

TypedArray attributesArray = context.obtainStyledAttributes(attrs, R.styleable.NumberPicker, defStyle, 0);

mSelectionDivider = getResources().getDrawable(R.drawable.np_numberpicker_selection_divider);
mSelectionDivider = ContextCompat.getDrawable(context, R.drawable.np_numberpicker_selection_divider);

mSelectionDividerColor = attributesArray.getColor(R.styleable.NumberPicker_np_dividerColor, mSelectionDividerColor);

Expand Down Expand Up @@ -593,7 +601,7 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
// draw() method to be called. Therefore, we declare we will draw.
setWillNotDraw(false);

LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.number_picker_with_selector_wheel, this, true);

// input text
Expand Down Expand Up @@ -648,8 +656,8 @@ public NumberPicker(Context context, AttributeSet attrs, int defStyle) {
mMaximumFlingVelocity = configuration.getScaledMaximumFlingVelocity() / SELECTOR_MAX_FLING_VELOCITY_ADJUSTMENT;

// create the fling and adjust scrollers
mFlingScroller = new Scroller(getContext(), null, true);
mAdjustScroller = new Scroller(getContext(), new DecelerateInterpolator(2.5f));
mFlingScroller = new Scroller(context, null, true);
mAdjustScroller = new Scroller(context, new DecelerateInterpolator(2.5f));

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
// If not explicitly specified this view is important for accessibility.
Expand Down Expand Up @@ -750,6 +758,10 @@ private boolean moveToFinalScrollerPosition(Scroller scroller) {

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}

final int action = event.getAction() & MotionEvent.ACTION_MASK;
switch (action) {
case MotionEvent.ACTION_DOWN: {
Expand Down Expand Up @@ -783,9 +795,9 @@ public boolean onInterceptTouchEvent(MotionEvent event) {
} else if (!mAdjustScroller.isFinished()) {
mFlingScroller.forceFinished(true);
mAdjustScroller.forceFinished(true);
} else if (mLastDownEventX < mLeftOfSelectionDividerLeft) {
} else if (mLastDownEventY < mTopSelectionDividerTop) {
postChangeCurrentByOneFromLongPress(false, ViewConfiguration.getLongPressTimeout());
} else if (mLastDownEventX > mRightOfSelectionDividerRight) {
} else if (mLastDownEventY > mBottomSelectionDividerBottom) {
postChangeCurrentByOneFromLongPress(true, ViewConfiguration.getLongPressTimeout());
}
return true;
Expand Down Expand Up @@ -951,7 +963,7 @@ public boolean dispatchTrackballEvent(MotionEvent event) {

@Override
protected boolean dispatchHoverEvent(MotionEvent event) {
if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
if (((AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
final int hoveredVirtualViewId;
if (isHorizontalMode()) {
final int eventX = (int) event.getX();
Expand Down Expand Up @@ -2314,7 +2326,7 @@ public void sendAccessibilityEventForVirtualView(int virtualViewId, int eventTyp
}

private void sendAccessibilityEventForVirtualText(int eventType) {
if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
if (((AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
mInputText.onInitializeAccessibilityEvent(event);
mInputText.onPopulateAccessibilityEvent(event);
Expand All @@ -2324,10 +2336,10 @@ private void sendAccessibilityEventForVirtualText(int eventType) {
}

private void sendAccessibilityEventForVirtualButton(int virtualViewId, int eventType, String text) {
if (((AccessibilityManager) getContext().getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
if (((AccessibilityManager) mContext.getSystemService(Context.ACCESSIBILITY_SERVICE)).isEnabled()) {
AccessibilityEvent event = AccessibilityEvent.obtain(eventType);
event.setClassName(Button.class.getName());
event.setPackageName(getContext().getPackageName());
event.setPackageName(mContext.getPackageName());
event.getText().add(text);
event.setEnabled(NumberPicker.this.isEnabled());
event.setSource(NumberPicker.this, virtualViewId);
Expand Down Expand Up @@ -2382,7 +2394,7 @@ private AccessibilityNodeInfo createAccessibilityNodeInfoForVirtualButton(int vi
String text, int left, int top, int right, int bottom) {
AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain();
info.setClassName(Button.class.getName());
info.setPackageName(getContext().getPackageName());
info.setPackageName(mContext.getPackageName());
info.setSource(NumberPicker.this, virtualViewId);
info.setParent(NumberPicker.this);
info.setText(text);
Expand Down Expand Up @@ -2415,7 +2427,7 @@ private AccessibilityNodeInfo createAccessibilityNodeInfoForVirtualButton(int vi
private AccessibilityNodeInfo createAccessibilityNodeInfoForNumberPicker(int left, int top, int right, int bottom) {
AccessibilityNodeInfo info = AccessibilityNodeInfo.obtain();
info.setClassName(NumberPicker.class.getName());
info.setPackageName(getContext().getPackageName());
info.setPackageName(mContext.getPackageName());
info.setSource(NumberPicker.this);

if (hasVirtualDecrementButton()) {
Expand All @@ -2431,8 +2443,7 @@ private AccessibilityNodeInfo createAccessibilityNodeInfoForNumberPicker(int lef
info.setScrollable(true);

/** TODO: Figure out compat implementation for this
final float applicationScale =
getContext().getResources().getCompatibilityInfo().applicationScale;
final float applicationScale = mContext.getResources().getCompatibilityInfo().applicationScale;
Rect boundsInParent = mTempRect;
boundsInParent.set(left, top, right, bottom);
Expand Down Expand Up @@ -2524,7 +2535,7 @@ public void setDividerColor(@ColorInt int color) {
}

public void setDividerColorResource(@ColorRes int colorId) {
setDividerColor(getResources().getColor(colorId));
setDividerColor(ContextCompat.getColor(mContext, colorId));
}

public void setDividerDistance(int distance) {
Expand Down Expand Up @@ -2565,7 +2576,7 @@ public void setTextColor(@ColorInt int color) {
}

public void setTextColorResource(@ColorRes int colorId) {
setTextColor(getResources().getColor(colorId));
setTextColor(ContextCompat.getColor(mContext, colorId));
}

public void setTextSize(float textSize) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import android.graphics.Typeface;
import android.os.Bundle;
import android.support.v4.content.ContextCompat;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;

Expand All @@ -19,15 +20,15 @@ protected void onCreate(Bundle savedInstanceState) {
NumberPicker numberPicker = (NumberPicker) findViewById(R.id.number_picker);

// set divider color
numberPicker.setDividerColor(getResources().getColor(R.color.colorPrimary));
numberPicker.setDividerColor(ContextCompat.getColor(this, R.color.colorPrimary));
numberPicker.setDividerColorResource(R.color.colorPrimary);

// set formatter
numberPicker.setFormatter(getString(R.string.number_picker_formatter));
numberPicker.setFormatter(R.string.number_picker_formatter);

// set text color
numberPicker.setTextColor(getResources().getColor(R.color.colorPrimary));
numberPicker.setTextColor(ContextCompat.getColor(this, R.color.colorPrimary));
numberPicker.setTextColorResource(R.color.colorPrimary);

// set text size
Expand Down

0 comments on commit afefd41

Please sign in to comment.