diff --git a/README.md b/README.md index 7f4cdf4..c7bf97b 100644 --- a/README.md +++ b/README.md @@ -135,6 +135,7 @@ Get **Progress** in Java mSlider.getProgress(); // The same as `getMinProgress`. mSlider.getMinProgress(); mSlider.getMaxProgress(); +mSlider.setValueChangedImmediately(true); // Default is false mSlider.setOnValueChangedListener(new DiscreteSlider.OnValueChangedListener() { @Override diff --git a/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java b/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java index 01edc41..301a870 100644 --- a/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java +++ b/discreteslider/src/main/java/hearsilent/discreteslider/DiscreteSlider.java @@ -99,6 +99,7 @@ public class DiscreteSlider extends View { @OrientationMode private int mOrientation; private OnValueChangedListener mListener; + private boolean mValueChangedImmediately = false; @IntDef({MODE_NORMAL, MODE_RANGE}) @Retention(RetentionPolicy.SOURCE) private @interface Mode { @@ -513,9 +514,8 @@ public void setMaxProgress(int progress) { checkProgressBound(); if (_progress != mMaxProgress && mListener != null) { if (mMaxProgress != -1 && mMode != MODE_NORMAL) { - mListener.onValueChanged(mMinProgress + mProgressOffset, mMaxProgress, false); - } else { - mListener.onValueChanged(mMinProgress + mProgressOffset, false); + mListener.onValueChanged(mMinProgress + mProgressOffset, + mMaxProgress + mProgressOffset, false); } } @@ -554,6 +554,10 @@ public void setOnValueChangedListener(@Nullable OnValueChangedListener listener) mListener = listener; } + public void setValueChangedImmediately(boolean immediately) { + mValueChangedImmediately = immediately; + } + private void generateInactiveTrackPath() { float radius = mTrackWidth / 2f; float left, top, right, bottom; @@ -1165,6 +1169,14 @@ protected void onDraw(Canvas canvas) { if (isHapticFeedbackEnabled()) { performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); } + if (mListener != null && mValueChangedImmediately) { + if (mMaxProgress != -1 && mMode != MODE_NORMAL) { + mListener.onValueChanged(progress + mProgressOffset, + mMaxProgress + mProgressOffset, true); + } else { + mListener.onValueChanged(progress + mProgressOffset, true); + } + } } if (mMaxProgress != -1 && mMode != MODE_NORMAL) { @@ -1193,6 +1205,12 @@ protected void onDraw(Canvas canvas) { if (isHapticFeedbackEnabled()) { performHapticFeedback(HapticFeedbackConstants.VIRTUAL_KEY); } + if (mListener != null && mValueChangedImmediately) { + if (mMaxProgress != -1 && mMode != MODE_NORMAL) { + mListener.onValueChanged(mMinProgress + mProgressOffset, + progress + mProgressOffset, true); + } + } } }