Created
September 1, 2020 11:04
-
-
Save KryptKode/1f3909c8aa2aa0378e04c8242306bbe6 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.global.gomoney.ui.cardcustomise | |
import android.content.res.ColorStateList | |
import android.view.LayoutInflater | |
import android.view.ViewGroup | |
import androidx.annotation.ColorRes | |
import androidx.core.content.res.ResourcesCompat | |
import androidx.core.view.isVisible | |
import androidx.core.widget.ImageViewCompat | |
import androidx.recyclerview.widget.RecyclerView | |
import com.global.gomoney.R | |
import com.global.gomoney.databinding.ItemLayoutVirtualCardSelectColorBinding | |
class CardCustomiseAdapter : RecyclerView.Adapter<CardCustomiseAdapter.ViewHolder>() { | |
val colors = arrayOf(R.color.darkIndigo, R.color.purply_purple, R.color.peachy_pink, R.color.maize, R.color.blue_green) | |
@ColorRes | |
var selectedColor = R.color.blue_green | |
lateinit var onColorSelected: (Int) -> Unit | |
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder = | |
ViewHolder(ItemLayoutVirtualCardSelectColorBinding.bind( | |
LayoutInflater.from(parent.context) | |
.inflate(R.layout.item_layout_virtual_card_select_color, parent, false) | |
)) | |
override fun getItemCount(): Int = colors.size | |
override fun onBindViewHolder(holder: ViewHolder, position: Int) { | |
holder.bind() | |
} | |
inner class ViewHolder(private val binding: ItemLayoutVirtualCardSelectColorBinding) : RecyclerView.ViewHolder(binding.root) { | |
init { | |
binding.root.setOnClickListener { | |
if (selectedColor != colors[adapterPosition]) { | |
selectedColor = colors[adapterPosition] | |
onColorSelected(selectedColor) | |
notifyDataSetChanged() | |
} | |
} | |
} | |
fun bind() { | |
ImageViewCompat.setImageTintList( | |
binding.cardColor, | |
ColorStateList.valueOf(ResourcesCompat.getColor(binding.root.resources, colors[adapterPosition], binding.root.context.theme))) | |
binding.selectedBg.isVisible = selectedColor == colors[adapterPosition] | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment