From 440428b2724a93438404678f4d9fdb40149c620f Mon Sep 17 00:00:00 2001 From: Priyansh Kedia Date: Sat, 20 Jun 2020 16:17:48 +0530 Subject: [PATCH] added attributes --- .../kedia/scaleselector/RecyclerAdapter.kt | 53 +++++++++++++--- .../kedia/scaleselector/ScaleSelector.java | 60 +++++++++++++++---- .../res/layout/main_recycler_view_layout.xml | 6 +- .../src/main/res/values/attrs.xml | 18 +++--- .../com/kedia/scaleselector/MainActivity.kt | 5 +- app/src/main/res/layout/activity_main.xml | 11 +++- 6 files changed, 112 insertions(+), 41 deletions(-) diff --git a/app/scaleselector/src/main/java/com/kedia/scaleselector/RecyclerAdapter.kt b/app/scaleselector/src/main/java/com/kedia/scaleselector/RecyclerAdapter.kt index 047b53c..850f6c3 100644 --- a/app/scaleselector/src/main/java/com/kedia/scaleselector/RecyclerAdapter.kt +++ b/app/scaleselector/src/main/java/com/kedia/scaleselector/RecyclerAdapter.kt @@ -10,7 +10,14 @@ import androidx.constraintlayout.widget.ConstraintLayout import androidx.recyclerview.widget.RecyclerView class RecyclerAdapter(private val list: MutableList, private val listener: OnClick, private val context: Context) : RecyclerView.Adapter() { + + 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)) @@ -23,7 +30,7 @@ class RecyclerAdapter(private val list: MutableList, 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 } } @@ -33,6 +40,30 @@ class RecyclerAdapter(private val list: MutableList, 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) @@ -41,7 +72,9 @@ class RecyclerAdapter(private val list: MutableList, 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 { @@ -54,31 +87,33 @@ class RecyclerAdapter(private val list: MutableList, 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) } } diff --git a/app/scaleselector/src/main/java/com/kedia/scaleselector/ScaleSelector.java b/app/scaleselector/src/main/java/com/kedia/scaleselector/ScaleSelector.java index ea25c78..08cf32d 100644 --- a/app/scaleselector/src/main/java/com/kedia/scaleselector/ScaleSelector.java +++ b/app/scaleselector/src/main/java/com/kedia/scaleselector/ScaleSelector.java @@ -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); @@ -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(); } @@ -68,10 +77,31 @@ private void initLayout(AttributeSet attributeSet) { } List 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 + android:layout_width="wrap_content" + android:layout_height="wrap_content"> - - - - - - - - - - - + + + + + + + \ No newline at end of file diff --git a/app/src/main/java/com/kedia/scaleselector/MainActivity.kt b/app/src/main/java/com/kedia/scaleselector/MainActivity.kt index 14abd85..583af83 100644 --- a/app/src/main/java/com/kedia/scaleselector/MainActivity.kt +++ b/app/src/main/java/com/kedia/scaleselector/MainActivity.kt @@ -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 @@ -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) } } \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 3d32317..3f0fcec 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -7,10 +7,15 @@ tools:context=".MainActivity">