diff --git a/build.gradle b/build.gradle index 7a8aaab..a3330d4 100644 --- a/build.gradle +++ b/build.gradle @@ -5,7 +5,7 @@ buildscript { jcenter() } dependencies { - classpath 'com.android.tools.build:gradle:2.2.0-rc1' + classpath 'com.android.tools.build:gradle:2.2.0' // NOTE: Do not place your application dependencies here; they belong // in the individual module build.gradle files diff --git a/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSeekBar.java b/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSeekBar.java index f2a4db6..953acfd 100644 --- a/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSeekBar.java +++ b/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSeekBar.java @@ -54,7 +54,7 @@ public DiscreteSeekBar(Context context, AttributeSet attrs, int defStyleAttr) { // endregion // region Helper Methods - private void init(Context context, AttributeSet attrs){ + private void init(Context context, AttributeSet attrs) { setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() { @Override public void onStartTrackingTouch(SeekBar seekBar) { @@ -63,53 +63,78 @@ public void onStartTrackingTouch(SeekBar seekBar) { @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { - if(fromUser) - fromUserCount+=1; + if (fromUser) + fromUserCount += 1; } @Override public void onStopTrackingTouch(final SeekBar seekBar) { int oldProgress = seekBar.getProgress(); final int newProgress; - if((oldProgress % stepSize) >= stepSize/2F){ - newProgress = (int)(((oldProgress/(int)stepSize)+1)*stepSize); + if ((oldProgress % stepSize) >= stepSize / 2F) { + newProgress = (int) (((oldProgress / (int) stepSize) + 1) * stepSize); } else { - newProgress = (int)(((oldProgress/(int)stepSize))*stepSize); + newProgress = (int) (((oldProgress / (int) stepSize)) * stepSize); } - if(fromUserCount>1){ // SeekBar Dragged + if (fromUserCount > 1) { // SeekBar Dragged ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, oldProgress, newProgress); animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); animation.setInterpolator(new DecelerateInterpolator()); animation.start(); } else { // SeekBar Clicked - ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, superOldProgress, newProgress); - animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); - animation.setInterpolator(new DecelerateInterpolator()); - animation.start(); + doAnimation(seekBar, newProgress); } fromUserCount = 0; - if(onDiscreteSeekBarChangeListener != null){ - onDiscreteSeekBarChangeListener.onPositionChanged(newProgress/MULTIPLIER); + if (onDiscreteSeekBarChangeListener != null) { + onDiscreteSeekBarChangeListener.onPositionChanged(newProgress / MULTIPLIER); } } }); } + private void doAnimation(SeekBar seekBar, int newProgress) { + ObjectAnimator animation = ObjectAnimator.ofInt(seekBar, PROGRESS_PROPERTY, superOldProgress, newProgress); + animation.setDuration(getResources().getInteger(android.R.integer.config_mediumAnimTime)); + animation.setInterpolator(new DecelerateInterpolator()); + animation.start(); + } + public void setTickMarkCount(int tickMarkCount) { this.tickMarkCount = tickMarkCount < 2 ? 2 : tickMarkCount; - setMax((this.tickMarkCount-1) * MULTIPLIER); - this.stepSize = getMax()/(this.tickMarkCount-1); + setMax((this.tickMarkCount - 1) * MULTIPLIER); + this.stepSize = getMax() / (this.tickMarkCount - 1); } - public void setOnDiscreteSeekBarChangeListener(OnDiscreteSeekBarChangeListener onDiscreteSeekBarChangeListener){ + public void setOnDiscreteSeekBarChangeListener(OnDiscreteSeekBarChangeListener onDiscreteSeekBarChangeListener) { this.onDiscreteSeekBarChangeListener = onDiscreteSeekBarChangeListener; } - public void setPosition(int position){ - this.setProgress(position*(int)stepSize); + + public void setPosition(int position) { + int progress = position * (int) stepSize; + this.setProgress(progress); + } + + public void setPositionAnimated(int position) { + + superOldProgress = getProgress(); + + int oldProgress = position * (int) stepSize; + + final int newProgress; + + if ((oldProgress % stepSize) >= stepSize / 2F) { + newProgress = (int) (((oldProgress / (int) stepSize) + 1) * stepSize); + } else { + newProgress = (int) (((oldProgress / (int) stepSize)) * stepSize); + } + + doAnimation(this, newProgress); + this.setProgress(newProgress); } + // endregion diff --git a/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSlider.java b/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSlider.java index b004299..204be0b 100644 --- a/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSlider.java +++ b/library/src/main/java/com/etiennelawlor/discreteslider/library/ui/DiscreteSlider.java @@ -62,7 +62,7 @@ public DiscreteSlider(Context context, AttributeSet attrs, int defStyleAttr) { // endregion // region Helper Methods - private void init(Context context, AttributeSet attrs){ + private void init(Context context, AttributeSet attrs) { TypedArray attributeArray = context.obtainStyledAttributes( attrs, R.styleable.DiscreteSlider); @@ -71,6 +71,7 @@ private void init(Context context, AttributeSet attrs){ tickMarkCount = attributeArray.getInteger(R.styleable.DiscreteSlider_tickMarkCount, 5); tickMarkRadius = attributeArray.getDimension(R.styleable.DiscreteSlider_tickMarkRadius, 8); position = attributeArray.getInteger(R.styleable.DiscreteSlider_position, 0); + horizontalBarThickness = attributeArray.getDimension(R.styleable.DiscreteSlider_horizontalBarThickness, 4); backdropFillColor = attributeArray.getColor(R.styleable.DiscreteSlider_backdropFillColor, Color.GRAY); backdropStrokeColor = attributeArray.getColor(R.styleable.DiscreteSlider_backdropStrokeColor, Color.GRAY); @@ -82,8 +83,8 @@ private void init(Context context, AttributeSet attrs){ } View view = inflate(context, R.layout.discrete_slider, this); - discreteSliderBackdrop = (DiscreteSliderBackdrop)view.findViewById(R.id.discrete_slider_backdrop); - discreteSeekBar = (DiscreteSeekBar)view.findViewById(R.id.discrete_seek_bar); + discreteSliderBackdrop = (DiscreteSliderBackdrop) view.findViewById(R.id.discrete_slider_backdrop); + discreteSeekBar = (DiscreteSeekBar) view.findViewById(R.id.discrete_seek_bar); setTickMarkCount(tickMarkCount); setTickMarkRadius(tickMarkRadius); @@ -95,61 +96,81 @@ private void init(Context context, AttributeSet attrs){ setThumb(thumb); setProgressDrawable(progressDrawable); - discreteSeekBar.setPadding(discreteSeekBarLeftPadding,0,discreteSeekBarRightPadding,0); + discreteSeekBar.setPadding(discreteSeekBarLeftPadding, 0, discreteSeekBarRightPadding, 0); discreteSeekBar.setOnDiscreteSeekBarChangeListener(new DiscreteSeekBar.OnDiscreteSeekBarChangeListener() { @Override public void onPositionChanged(int position) { - if(onDiscreteSliderChangeListener != null){ + if (onDiscreteSliderChangeListener != null) { onDiscreteSliderChangeListener.onPositionChanged(position); } } }); } - public void setTickMarkCount(int tickMarkCount){ + public void setTickMarkCount(int tickMarkCount) { discreteSliderBackdrop.setTickMarkCount(tickMarkCount); discreteSeekBar.setTickMarkCount(tickMarkCount); } - public void setTickMarkRadius(float tickMarkRadius){ + public void setTickMarkRadius(float tickMarkRadius) { discreteSliderBackdrop.setTickMarkRadius(tickMarkRadius); } - public void setPosition(int position) { - if(position<0){ + private void validatePsition(int position) { + if (position < 0) { this.position = 0; - } else if(position>tickMarkCount-1){ - this.position = tickMarkCount-1; + } else if (position > tickMarkCount - 1) { + this.position = tickMarkCount - 1; } else { this.position = position; } + } + + public void setPosition(int position) { + validatePsition(position); discreteSeekBar.setPosition(this.position); + if (onDiscreteSliderChangeListener != null) { + onDiscreteSliderChangeListener.onPositionChanged(position); + } + } + + /** + * Sets the current position of mark and animate it + * + * @param position + */ + public void setPositionAnimated(int position) { + validatePsition(position); + discreteSeekBar.setPositionAnimated(this.position); + if (onDiscreteSliderChangeListener != null) { + onDiscreteSliderChangeListener.onPositionChanged(position); + } } - public void setHorizontalBarThickness(float horizontalBarThickness){ + public void setHorizontalBarThickness(float horizontalBarThickness) { discreteSliderBackdrop.setHorizontalBarThickness(horizontalBarThickness); } - public void setBackdropFillColor(int backdropFillColor){ + public void setBackdropFillColor(int backdropFillColor) { discreteSliderBackdrop.setBackdropFillColor(backdropFillColor); } - public void setBackdropStrokeColor(int backdropStrokeColor){ + public void setBackdropStrokeColor(int backdropStrokeColor) { discreteSliderBackdrop.setBackdropStrokeColor(backdropStrokeColor); } - public void setBackdropStrokeWidth(float backdropStrokeWidth){ + public void setBackdropStrokeWidth(float backdropStrokeWidth) { discreteSliderBackdrop.setBackdropStrokeWidth(backdropStrokeWidth); } - public void setThumb(Drawable thumb){ - if(thumb != null) + public void setThumb(Drawable thumb) { + if (thumb != null) discreteSeekBar.setThumb(thumb); } - public void setProgressDrawable(Drawable progressDrawable){ - if(progressDrawable != null) + public void setProgressDrawable(Drawable progressDrawable) { + if (progressDrawable != null) discreteSeekBar.setProgressDrawable(progressDrawable); } diff --git a/sample/src/main/java/com/etiennelawlor/discreteslider/fragments/MainFragment.java b/sample/src/main/java/com/etiennelawlor/discreteslider/fragments/MainFragment.java index 75bf7d5..4495dc0 100644 --- a/sample/src/main/java/com/etiennelawlor/discreteslider/fragments/MainFragment.java +++ b/sample/src/main/java/com/etiennelawlor/discreteslider/fragments/MainFragment.java @@ -3,6 +3,9 @@ import android.os.Bundle; import android.support.annotation.Nullable; import android.support.v4.app.Fragment; +import android.support.v4.app.FragmentManager; +import android.support.v4.app.FragmentPagerAdapter; +import android.support.v4.view.ViewPager; import android.view.Gravity; import android.view.LayoutInflater; import android.view.View; @@ -16,6 +19,9 @@ import com.etiennelawlor.discreteslider.library.ui.DiscreteSlider; import com.etiennelawlor.discreteslider.library.utilities.DisplayUtility; +import java.util.ArrayList; +import java.util.List; + import butterknife.Bind; import butterknife.ButterKnife; @@ -30,6 +36,10 @@ public class MainFragment extends Fragment { DiscreteSlider discreteSlider; @Bind(R.id.tick_mark_labels_rl) RelativeLayout tickMarkLabelsRelativeLayout; + @Bind(R.id.pager) + ViewPager viewPager; + + ViewPagerAdapter adapter; // endregion // region Constructors @@ -56,6 +66,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, View rootView = inflater.inflate(R.layout.fragment_main, container, false); ButterKnife.bind(this, rootView); + return rootView; } @@ -63,6 +74,8 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); + adapter = new ViewPagerAdapter(getFragmentManager()); + // Dynamically update attributes // discreteSlider.setTickMarkCount(10); // discreteSlider.setTickMarkRadius(16); @@ -78,9 +91,10 @@ public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { @Override public void onPositionChanged(int position) { int childCount = tickMarkLabelsRelativeLayout.getChildCount(); - for(int i= 0; i fragmentList = new ArrayList<>(); + private final List fragmentTitles = new ArrayList<>(); + + public ViewPagerAdapter(FragmentManager fm) { + super(fm); + } + + public void addPage(Fragment fragment, String title) { + fragmentList.add(fragment); + fragmentTitles.add(title); + } + + public String getTitleAt(int position) { + return fragmentTitles.get(position); + } + + @Override + public Fragment getItem(int position) { + return fragmentList.get(position); + } + + @Override + public int getCount() { + return fragmentList.size(); + } + } + + // endregion } \ No newline at end of file diff --git a/sample/src/main/res/layout/fragment_main.xml b/sample/src/main/res/layout/fragment_main.xml index de4d454..ae6ee90 100644 --- a/sample/src/main/res/layout/fragment_main.xml +++ b/sample/src/main/res/layout/fragment_main.xml @@ -1,42 +1,49 @@ - + android:background="@color/grey_500" + android:orientation="vertical"> + + + android:background="@color/grey_100" + android:orientation="vertical"> + android:paddingTop="12dp" /> + app:tickMarkCount="3" + app:tickMarkRadius="8dp" /> - \ No newline at end of file + \ No newline at end of file