Skip to content

Commit

Permalink
Support fire onValueChanged immediately
Browse files Browse the repository at this point in the history
  • Loading branch information
hearsilent committed Apr 6, 2020
1 parent 96f3cc1 commit 4b9dd81
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 3 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 {

Expand Down Expand Up @@ -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);
}
}

Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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);
}
}
}

}
Expand Down

0 comments on commit 4b9dd81

Please sign in to comment.