Skip to content

Commit 440428b

Browse files
committed
added attributes
1 parent b0c7957 commit 440428b

File tree

6 files changed

+112
-41
lines changed

6 files changed

+112
-41
lines changed

app/scaleselector/src/main/java/com/kedia/scaleselector/RecyclerAdapter.kt

Lines changed: 44 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,14 @@ import androidx.constraintlayout.widget.ConstraintLayout
1010
import androidx.recyclerview.widget.RecyclerView
1111

1212
class RecyclerAdapter(private val list: MutableList<Int>, private val listener: OnClick, private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {
13+
14+
private var pointerColor: Int = Color.WHITE
1315
private var selectedPosition = -1
16+
private var stepValue: Int = 5
17+
private var backgroundCardColor = Color.BLACK
18+
private var defaultPointerColor = Color.WHITE
19+
private var defaultTextColor = Color.WHITE
20+
private var selectedTextColor = Color.WHITE
1421

1522
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): RecyclerView.ViewHolder {
1623
return ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.biometrics_selector_card, parent, false))
@@ -23,7 +30,7 @@ class RecyclerAdapter(private val list: MutableList<Int>, private val listener:
2330
override fun onBindViewHolder(holder: RecyclerView.ViewHolder, position: Int) {
2431
if (holder is ViewHolder) {
2532
holder.bind(list[position])
26-
if (holder.itemView.isSelected)
33+
if (holder.itemView.elevation == 2f)
2734
selectedPosition = position
2835
}
2936
}
@@ -33,6 +40,30 @@ class RecyclerAdapter(private val list: MutableList<Int>, private val listener:
3340
notifyDataSetChanged()
3441
}
3542

43+
fun setPointerColor(color: Int) {
44+
this.pointerColor = color
45+
}
46+
47+
fun setStepValue(stepValue: Int) {
48+
this.stepValue = stepValue
49+
}
50+
51+
fun setBackGroundCardColor(color: Int) {
52+
this.backgroundCardColor = color
53+
}
54+
55+
fun setDefaultPointerColor(color: Int) {
56+
this.defaultPointerColor = color
57+
}
58+
59+
fun setDefaultTextColor(color: Int) {
60+
this.defaultTextColor = color
61+
}
62+
63+
fun setSelectedTextColor(color: Int) {
64+
this.selectedTextColor = color
65+
}
66+
3667
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
3768

3869
private val selectorLayout: ConstraintLayout = itemView.findViewById(R.id.selectorLayout)
@@ -41,7 +72,9 @@ class RecyclerAdapter(private val list: MutableList<Int>, private val listener:
4172

4273
fun bind(int: Int) {
4374

44-
if (int % 5 == 0) {
75+
selectorLayout.setBackgroundColor(backgroundCardColor)
76+
77+
if (int % stepValue == 0) {
4578
heightText.text = int.toString()
4679
heightText.visibility = View.VISIBLE
4780
} else {
@@ -54,31 +87,33 @@ class RecyclerAdapter(private val list: MutableList<Int>, private val listener:
5487
scaleY = 1.2f
5588
}
5689
heightLine.scaleY = 2.5f
57-
itemView.isSelected = true
90+
itemView.elevation = 2f
5891
} else {
5992
heightText.apply {
6093
scaleY = 1f
94+
setTextColor(defaultTextColor)
6195
}
62-
itemView.isSelected = false
96+
itemView.elevation = 1f
6397
heightLine.scaleY = 1f
64-
heightLine.setBackgroundColor(Color.parseColor("#ffffff"))
65-
if (Integer.valueOf(heightText.text.toString().trim()) % 5 != 0)
98+
heightLine.setBackgroundColor(defaultPointerColor)
99+
if (Integer.valueOf(heightText.text.toString().trim()) % stepValue != 0)
66100
heightText.visibility = View.INVISIBLE
67101
}
68102

69103
itemView.setOnClickListener {
70-
it.isSelected = true
104+
it.elevation = 2f
71105
heightText.scaleY = 1.2f
106+
heightText.setTextColor(selectedTextColor)
72107
heightLine.apply {
73108
scaleY = 2.5f
74-
setBackgroundColor(Color.parseColor("#ffffff"))
109+
setBackgroundColor(pointerColor)
75110
}
76111
heightText.visibility = View.VISIBLE
77112
if (selectedPosition != -1)
78113
notifyItemChanged(selectedPosition)
79114
notifyItemChanged(selectedPosition)
80115
selectedPosition = adapterPosition
81-
listener.onHeightClicked(heightText.toString().trim(), adapterPosition)
116+
listener.onHeightClicked(heightText.text.toString().trim(), adapterPosition)
82117
}
83118
}
84119

app/scaleselector/src/main/java/com/kedia/scaleselector/ScaleSelector.java

Lines changed: 48 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,39 @@
22

33
import android.content.Context;
44
import android.content.res.TypedArray;
5+
import android.graphics.Color;
56
import android.util.AttributeSet;
6-
77
import android.view.LayoutInflater;
88
import android.view.View;
99
import android.widget.FrameLayout;
10-
import android.widget.LinearLayout;
1110

1211
import androidx.recyclerview.widget.LinearLayoutManager;
1312
import androidx.recyclerview.widget.RecyclerView;
13+
import androidx.recyclerview.widget.SimpleItemAnimator;
1414

1515
import org.jetbrains.annotations.NotNull;
1616

1717
import java.util.ArrayList;
18-
import java.util.Collections;
1918
import java.util.List;
2019

2120
public 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
}

app/scaleselector/src/main/res/layout/main_recycler_view_layout.xml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,12 @@
33
xmlns:app="http://schemas.android.com/apk/res-auto"
44
xmlns:tools="http://schemas.android.com/tools"
55
android:orientation="vertical"
6-
android:layout_width="match_parent"
7-
android:layout_height="match_parent">
6+
android:layout_width="wrap_content"
7+
android:layout_height="wrap_content">
88

99
<androidx.recyclerview.widget.RecyclerView
1010
android:id="@+id/recycler"
11-
android:layout_width="0dp"
11+
android:layout_width="match_parent"
1212
android:layout_height="wrap_content"
1313
android:orientation="horizontal"
1414
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"

app/scaleselector/src/main/res/values/attrs.xml

Lines changed: 7 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,16 +2,12 @@
22
<resources>
33
<declare-styleable name="ScaleSelector">
44
<attr name="stepValue" format="integer" />
5-
<attr name="orientation" format="enum">
6-
<enum name="horizontal" value="1"/>
7-
<enum name="vertical" value="2" />
8-
</attr>
9-
<attr name="pointerPosition" format="enum">
10-
<enum name="start" value="-1" />
11-
<enum name="centre" value="0" />
12-
<enum name="end" value="1" />
13-
</attr>
14-
<attr name="pointerColor" format="integer" />
15-
5+
<attr name="defaultTextColor" format="color" />
6+
<attr name="selectedTextColor" format="color" />
7+
<attr name="selectedPointerColor" format="color" />
8+
<attr name="minValue" format="integer" />
9+
<attr name="maxValue" format="integer" />
10+
<attr name="defaultPointerColor" format="color" />
11+
<attr name="backgroundColor" format="color" />
1612
</declare-styleable>
1713
</resources>

app/src/main/java/com/kedia/scaleselector/MainActivity.kt

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@ package com.kedia.scaleselector
22

33
import androidx.appcompat.app.AppCompatActivity
44
import android.os.Bundle
5+
import android.os.Handler
6+
import android.util.Log
57
import android.widget.GridLayout
68
import android.widget.LinearLayout
79
import androidx.recyclerview.widget.LinearLayoutManager
@@ -11,8 +13,5 @@ class MainActivity : AppCompatActivity() {
1113
override fun onCreate(savedInstanceState: Bundle?) {
1214
super.onCreate(savedInstanceState)
1315
setContentView(R.layout.activity_main)
14-
15-
// val linearLayout = LinearLayoutManager(this)
16-
// testRecycler.setLayoutManager(linearLayout)
1716
}
1817
}

app/src/main/res/layout/activity_main.xml

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,15 @@
77
tools:context=".MainActivity">
88

99
<com.kedia.scaleselector.ScaleSelector
10-
android:layout_width="match_parent"
11-
android:layout_height="match_parent"
10+
android:layout_width="wrap_content"
11+
android:layout_height="wrap_content"
1212
android:id="@+id/testRecycler"
13-
app:orientation="vertical"
13+
app:minValue="100"
14+
app:maxValue="200"
15+
app:defaultPointerColor="#6F86D6"
16+
app:stepValue="10"
17+
app:selectedTextColor="#fff"
18+
app:defaultTextColor="#fff"
1419
app:layout_constraintStart_toStartOf="parent"
1520
app:layout_constraintEnd_toEndOf="parent"
1621
app:layout_constraintTop_toTopOf="parent"

0 commit comments

Comments
 (0)