22
33import android .content .Context ;
44import android .content .res .TypedArray ;
5+ import android .graphics .Color ;
56import android .util .AttributeSet ;
6-
77import android .view .LayoutInflater ;
88import android .view .View ;
99import android .widget .FrameLayout ;
10- import android .widget .LinearLayout ;
1110
1211import androidx .recyclerview .widget .LinearLayoutManager ;
1312import androidx .recyclerview .widget .RecyclerView ;
13+ import androidx .recyclerview .widget .SimpleItemAnimator ;
1414
1515import org .jetbrains .annotations .NotNull ;
1616
1717import java .util .ArrayList ;
18- import java .util .Collections ;
1918import java .util .List ;
2019
2120public class ScaleSelector extends FrameLayout implements RecyclerAdapter .OnClick {
2221
2322 private int stepValue ;
24- private int orientation ;
25- private int pointerPosition ;
23+ private int defaultTextColor ;
24+ private int selectedTextColor ;
2625 private int pointerColor ;
2726 private int mainLayoutId ;
27+ private int minValue ;
28+ private int backGroundColor ;
29+ private int defaultPointerColor ;
30+ private int maxValue ;
2831
2932 private RecyclerView mRecycler ;
33+ private RecyclerAdapter adapter ;
34+
35+ private LinearLayoutManager linearLayoutManager ;
3036
31- private LinearLayoutManager linearLayoutManager = new LinearLayoutManager ( getContext (), LinearLayoutManager . HORIZONTAL , false ) ;
37+ public int selectedValue ;
3238
3339 public ScaleSelector (Context context ) {
3440 super (context );
@@ -45,10 +51,13 @@ private void init(AttributeSet attributeSet) {
4551 try {
4652 mainLayoutId = R .layout .main_recycler_view_layout ;
4753 stepValue = typedArray .getInt (R .styleable .ScaleSelector_stepValue , 5 );
48- orientation = typedArray .getInt (R .styleable .ScaleSelector_orientation , 1 );
49- pointerPosition = typedArray .getInt (R .styleable .ScaleSelector_pointerPosition , 0 );
50- pointerColor = typedArray .getColor (R .styleable .ScaleSelector_pointerColor ,getContext ().getResources ().getColor (R .color .blue ));
51-
54+ backGroundColor = typedArray .getColor (R .styleable .ScaleSelector_backgroundColor , Color .parseColor ("#000000" ));
55+ pointerColor = typedArray .getColor (R .styleable .ScaleSelector_selectedPointerColor ,getContext ().getResources ().getColor (R .color .blue ));
56+ minValue = typedArray .getInt (R .styleable .ScaleSelector_minValue , 0 );
57+ maxValue = typedArray .getInt (R .styleable .ScaleSelector_maxValue , 200 );
58+ defaultPointerColor = typedArray .getColor (R .styleable .ScaleSelector_defaultPointerColor , Color .parseColor ("#ffffff" ));
59+ defaultTextColor = typedArray .getColor (R .styleable .ScaleSelector_defaultTextColor , Color .parseColor ("#ffffff" ));
60+ selectedTextColor = typedArray .getColor (R .styleable .ScaleSelector_selectedTextColor , Color .parseColor ("#ffffff" ));
5261 } finally {
5362 typedArray .recycle ();
5463 }
@@ -68,10 +77,31 @@ private void initLayout(AttributeSet attributeSet) {
6877 }
6978
7079 List <Integer > list = new ArrayList ();
71- for (int i =0 ; i <200 ; i ++)
80+
81+ if (!(minValue < maxValue )) {
82+ throw new IllegalArgumentException ("Minimum value cannot be greater than maximum value" );
83+ }
84+
85+ for (int i =minValue ; i <maxValue ; i ++)
7286 list .add (i );
7387
74- mRecycler .setAdapter (new RecyclerAdapter (list ,this ,getContext ()));
88+
89+
90+ adapter = new RecyclerAdapter (list ,this ,getContext ());
91+ adapter .setPointerColor (pointerColor );
92+ adapter .setStepValue (stepValue );
93+ adapter .setBackGroundCardColor (backGroundColor );
94+ adapter .setDefaultPointerColor (defaultPointerColor );
95+ adapter .setDefaultTextColor (defaultTextColor );
96+ adapter .setSelectedTextColor (selectedTextColor );
97+ mRecycler .setAdapter (adapter );
98+ RecyclerView .ItemAnimator animator = mRecycler .getItemAnimator ();
99+ if (animator instanceof SimpleItemAnimator ) {
100+ ((SimpleItemAnimator ) animator ).setSupportsChangeAnimations (false );
101+ ((SimpleItemAnimator ) animator ).setChangeDuration (100 );
102+ }
103+ linearLayoutManager = new LinearLayoutManager (getContext (), LinearLayoutManager .HORIZONTAL , false );
104+
75105 mRecycler .setLayoutManager (linearLayoutManager );
76106 }
77107
@@ -96,11 +126,17 @@ private void setAdapterInternal(RecyclerView.Adapter adapter, boolean compatible
96126 mRecycler .setAdapter (adapter );
97127 }
98128
129+ public int getSelectedValue () {
130+ return selectedValue ;
131+ }
132+
99133 @ Override
100134 public void onHeightClicked (@ NotNull String height , int adapterPosition ) {
101135// val center = heightRecycler.width /2 - heightRecycler.findViewHolderForAdapterPosition(adapterPosition)?.itemView?.width!! / 2
102136// heightLayoutManager.scrollToPositionWithOffset(adapterPosition, center)
103137 int center = mRecycler .getWidth () / 2 - mRecycler .findViewHolderForAdapterPosition (adapterPosition ).itemView .getWidth () / 2 ;
104138 linearLayoutManager .scrollToPositionWithOffset (adapterPosition , center );
139+ selectedValue = Integer .parseInt (height );
105140 }
141+
106142}
0 commit comments