Skip to content

Commit

Permalink
added attributes
Browse files Browse the repository at this point in the history
  • Loading branch information
Priyansh-Kedia committed Jun 20, 2020
1 parent b0c7957 commit 440428b
Show file tree
Hide file tree
Showing 6 changed files with 112 additions and 41 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@ import androidx.constraintlayout.widget.ConstraintLayout
import androidx.recyclerview.widget.RecyclerView

class RecyclerAdapter(private val list: MutableList<Int>, private val listener: OnClick, private val context: Context) : RecyclerView.Adapter<RecyclerView.ViewHolder>() {

private var pointerColor: Int = Color.WHITE
private var selectedPosition = -1
private var stepValue: Int = 5
private var backgroundCardColor = Color.BLACK
private var defaultPointerColor = Color.WHITE
private var defaultTextColor = Color.WHITE
private var selectedTextColor = Color.WHITE

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

fun setPointerColor(color: Int) {
this.pointerColor = color
}

fun setStepValue(stepValue: Int) {
this.stepValue = stepValue
}

fun setBackGroundCardColor(color: Int) {
this.backgroundCardColor = color
}

fun setDefaultPointerColor(color: Int) {
this.defaultPointerColor = color
}

fun setDefaultTextColor(color: Int) {
this.defaultTextColor = color
}

fun setSelectedTextColor(color: Int) {
this.selectedTextColor = color
}

inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {

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

fun bind(int: Int) {

if (int % 5 == 0) {
selectorLayout.setBackgroundColor(backgroundCardColor)

if (int % stepValue == 0) {
heightText.text = int.toString()
heightText.visibility = View.VISIBLE
} else {
Expand All @@ -54,31 +87,33 @@ class RecyclerAdapter(private val list: MutableList<Int>, private val listener:
scaleY = 1.2f
}
heightLine.scaleY = 2.5f
itemView.isSelected = true
itemView.elevation = 2f
} else {
heightText.apply {
scaleY = 1f
setTextColor(defaultTextColor)
}
itemView.isSelected = false
itemView.elevation = 1f
heightLine.scaleY = 1f
heightLine.setBackgroundColor(Color.parseColor("#ffffff"))
if (Integer.valueOf(heightText.text.toString().trim()) % 5 != 0)
heightLine.setBackgroundColor(defaultPointerColor)
if (Integer.valueOf(heightText.text.toString().trim()) % stepValue != 0)
heightText.visibility = View.INVISIBLE
}

itemView.setOnClickListener {
it.isSelected = true
it.elevation = 2f
heightText.scaleY = 1.2f
heightText.setTextColor(selectedTextColor)
heightLine.apply {
scaleY = 2.5f
setBackgroundColor(Color.parseColor("#ffffff"))
setBackgroundColor(pointerColor)
}
heightText.visibility = View.VISIBLE
if (selectedPosition != -1)
notifyItemChanged(selectedPosition)
notifyItemChanged(selectedPosition)
selectedPosition = adapterPosition
listener.onHeightClicked(heightText.toString().trim(), adapterPosition)
listener.onHeightClicked(heightText.text.toString().trim(), adapterPosition)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,39 @@

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.util.AttributeSet;

import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;
import androidx.recyclerview.widget.SimpleItemAnimator;

import org.jetbrains.annotations.NotNull;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class ScaleSelector extends FrameLayout implements RecyclerAdapter.OnClick{

private int stepValue;
private int orientation;
private int pointerPosition;
private int defaultTextColor;
private int selectedTextColor;
private int pointerColor;
private int mainLayoutId;
private int minValue;
private int backGroundColor;
private int defaultPointerColor;
private int maxValue;

private RecyclerView mRecycler;
private RecyclerAdapter adapter;

private LinearLayoutManager linearLayoutManager ;

private LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);
public int selectedValue;

public ScaleSelector(Context context) {
super(context);
Expand All @@ -45,10 +51,13 @@ private void init(AttributeSet attributeSet) {
try {
mainLayoutId = R.layout.main_recycler_view_layout;
stepValue = typedArray.getInt(R.styleable.ScaleSelector_stepValue, 5);
orientation = typedArray.getInt(R.styleable.ScaleSelector_orientation, 1);
pointerPosition = typedArray.getInt(R.styleable.ScaleSelector_pointerPosition, 0);
pointerColor = typedArray.getColor(R.styleable.ScaleSelector_pointerColor,getContext().getResources().getColor(R.color.blue));

backGroundColor = typedArray.getColor(R.styleable.ScaleSelector_backgroundColor, Color.parseColor("#000000"));
pointerColor = typedArray.getColor(R.styleable.ScaleSelector_selectedPointerColor,getContext().getResources().getColor(R.color.blue));
minValue = typedArray.getInt(R.styleable.ScaleSelector_minValue, 0);
maxValue = typedArray.getInt(R.styleable.ScaleSelector_maxValue, 200);
defaultPointerColor = typedArray.getColor(R.styleable.ScaleSelector_defaultPointerColor, Color.parseColor("#ffffff"));
defaultTextColor = typedArray.getColor(R.styleable.ScaleSelector_defaultTextColor, Color.parseColor("#ffffff"));
selectedTextColor = typedArray.getColor(R.styleable.ScaleSelector_selectedTextColor, Color.parseColor("#ffffff"));
} finally {
typedArray.recycle();
}
Expand All @@ -68,10 +77,31 @@ private void initLayout(AttributeSet attributeSet) {
}

List<Integer> list = new ArrayList();
for (int i=0; i<200; i++)

if (!(minValue < maxValue)) {
throw new IllegalArgumentException("Minimum value cannot be greater than maximum value");
}

for (int i=minValue; i<maxValue; i++)
list.add(i);

mRecycler.setAdapter(new RecyclerAdapter(list,this,getContext()));


adapter = new RecyclerAdapter(list,this,getContext());
adapter.setPointerColor(pointerColor);
adapter.setStepValue(stepValue);
adapter.setBackGroundCardColor(backGroundColor);
adapter.setDefaultPointerColor(defaultPointerColor);
adapter.setDefaultTextColor(defaultTextColor);
adapter.setSelectedTextColor(selectedTextColor);
mRecycler.setAdapter(adapter);
RecyclerView.ItemAnimator animator = mRecycler.getItemAnimator();
if (animator instanceof SimpleItemAnimator) {
((SimpleItemAnimator) animator).setSupportsChangeAnimations(false);
((SimpleItemAnimator) animator).setChangeDuration(100);
}
linearLayoutManager = new LinearLayoutManager(getContext(), LinearLayoutManager.HORIZONTAL, false);

mRecycler.setLayoutManager(linearLayoutManager);
}

Expand All @@ -96,11 +126,17 @@ private void setAdapterInternal(RecyclerView.Adapter adapter, boolean compatible
mRecycler.setAdapter(adapter);
}

public int getSelectedValue() {
return selectedValue;
}

@Override
public void onHeightClicked(@NotNull String height, int adapterPosition) {
// val center = heightRecycler.width /2 - heightRecycler.findViewHolderForAdapterPosition(adapterPosition)?.itemView?.width!! / 2
// heightLayoutManager.scrollToPositionWithOffset(adapterPosition, center)
int center = mRecycler.getWidth() / 2 - mRecycler.findViewHolderForAdapterPosition(adapterPosition).itemView.getWidth() / 2;
linearLayoutManager.scrollToPositionWithOffset(adapterPosition, center);
selectedValue = Integer.parseInt(height);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
android:layout_width="wrap_content"
android:layout_height="wrap_content">

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/recycler"
android:layout_width="0dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
Expand Down
18 changes: 7 additions & 11 deletions app/scaleselector/src/main/res/values/attrs.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,16 +2,12 @@
<resources>
<declare-styleable name="ScaleSelector">
<attr name="stepValue" format="integer" />
<attr name="orientation" format="enum">
<enum name="horizontal" value="1"/>
<enum name="vertical" value="2" />
</attr>
<attr name="pointerPosition" format="enum">
<enum name="start" value="-1" />
<enum name="centre" value="0" />
<enum name="end" value="1" />
</attr>
<attr name="pointerColor" format="integer" />

<attr name="defaultTextColor" format="color" />
<attr name="selectedTextColor" format="color" />
<attr name="selectedPointerColor" format="color" />
<attr name="minValue" format="integer" />
<attr name="maxValue" format="integer" />
<attr name="defaultPointerColor" format="color" />
<attr name="backgroundColor" format="color" />
</declare-styleable>
</resources>
5 changes: 2 additions & 3 deletions app/src/main/java/com/kedia/scaleselector/MainActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package com.kedia.scaleselector

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.os.Handler
import android.util.Log
import android.widget.GridLayout
import android.widget.LinearLayout
import androidx.recyclerview.widget.LinearLayoutManager
Expand All @@ -11,8 +13,5 @@ class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)

// val linearLayout = LinearLayoutManager(this)
// testRecycler.setLayoutManager(linearLayout)
}
}
11 changes: 8 additions & 3 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,15 @@
tools:context=".MainActivity">

<com.kedia.scaleselector.ScaleSelector
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/testRecycler"
app:orientation="vertical"
app:minValue="100"
app:maxValue="200"
app:defaultPointerColor="#6F86D6"
app:stepValue="10"
app:selectedTextColor="#fff"
app:defaultTextColor="#fff"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
Expand Down

0 comments on commit 440428b

Please sign in to comment.