Advertisement
zihadrizkyef

Untitled

Aug 18th, 2022
906
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.85 KB | None | 0 0
  1. package com.olserapratama.pos
  2.  
  3. import android.graphics.Color
  4. import android.view.LayoutInflater
  5. import android.view.View
  6. import android.view.ViewGroup
  7. import android.widget.ImageView
  8. import android.widget.TextView
  9. import androidx.constraintlayout.widget.ConstraintLayout
  10. import androidx.recyclerview.widget.RecyclerView
  11. import com.olserapratama.pos.R
  12. import com.olserapratama.pos.adapter.CustomerAdapter
  13. import com.olserapratama.pos.databinding.ListitemPpobGameBillBinding
  14. import com.olserapratama.pos.model.ppob.PpobTypeGroupDetail
  15. import mehdi.sakout.fancybuttons.FancyButton
  16. import java.text.DecimalFormat
  17.  
  18. class BillGameAdapter(var listGameBill: ArrayList<PpobTypeGroupDetail>) : RecyclerView.Adapter<BillGameAdapter.CustomerVH>() {
  19.  
  20.     var mCallback: Callback? = null
  21.  
  22.     fun setData(mList: ArrayList<PpobTypeGroupDetail>) {
  23.         this.listGameBill = mList
  24.         notifyDataSetChanged()
  25.     }
  26.  
  27.     interface Callback {
  28.         fun onItemClicked(index: Int)
  29.     }
  30.  
  31.     fun setCallback(mCallback: Callback) {
  32.         this.mCallback = mCallback
  33.     }
  34.  
  35.     override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomerAdapter.CustomerVH {
  36.         val binding = ListitemPpobGameBillBinding.inflate(LayoutInflater.from(parent.context), parent, false)
  37.         return CustomerAdapter.CustomerVH(binding, this)
  38.     }
  39.  
  40.     override fun onBindViewHolder(holder: CustomerAdapter.CustomerVH, position: Int) {
  41. //    val data = listGameBill[position]
  42.         holder.bind(listGameBill[position])
  43.         holder.clGameBill.setOnClickListener {
  44.             mCallback?.onItemClicked(position)
  45.         }
  46.     }
  47.  
  48.     override fun getItemCount(): Int {
  49.         var count = 0
  50.         if (listGameBill != null) count += listGameBill.size
  51.  
  52.         return count
  53.     }
  54.  
  55.     //nah pakai yang custom ini item clicknya
  56.     class CustomerVH(val binding: ListitemPpobGameBillBinding) :
  57.         RecyclerView.ViewHolder(binding.root) {
  58.         var tvTextName: TextView? = null
  59.         var tvTextPrice: TextView? = null
  60.         var clGameBill: ConstraintLayout? = null
  61.         private var selectedPosition: Int? = null
  62.  
  63.         init {
  64.             selectedPosition = null
  65.             tvTextName = binding.textName
  66.             tvTextPrice = binding.textPrice
  67.             clGameBill = binding.clGameBill
  68.         }
  69.  
  70.         fun bind(data: PpobTypeGroupDetail) {
  71.             val decimalFormat = DecimalFormat("##,###,###")
  72.             val hargaJual = data.hargaJual ?: 0
  73.  
  74.             with(binding) {
  75.                 tvTextName!!.text = data.detail
  76.                 tvTextPrice!!.text = data.fhargaJual
  77.                 selectedPosition = bindingAdapterPosition
  78.  
  79.                 clGameBill.setOnClickListener {
  80.                     selectedPosition = bindingAdapterPosition
  81.  
  82.                 }
  83.  
  84.                 when (selectedPosition) {
  85.                     null -> {
  86.                         tvTextName!!.setTextColor(Color.parseColor("#000000"))
  87.                         tvTextPrice!!.setTextColor(Color.parseColor("#808080"))
  88.                         clGameBill.background = root.context.getDrawable(R.drawable.bg_round_border_grey)
  89.                     }
  90.                     bindingAdapterPosition -> {
  91.                         tvTextName!!.setTextColor(Color.parseColor("#1976D2"))
  92.                         tvTextPrice!!.setTextColor(Color.parseColor("#4CAF50"))
  93.                         clGameBill.background = root.context.getDrawable(R.drawable.bg_round_border_grey_disable)
  94.                     }
  95.                     else -> {
  96.                         tvTextName!!.setTextColor(Color.parseColor("#000000"))
  97.                         tvTextPrice!!.setTextColor(Color.parseColor("#808080"))
  98.                         clGameBill.background = root.context.getDrawable(R.drawable.bg_round_border_grey)
  99.                     }
  100.                 }
  101.             }
  102.         }
  103.     }
  104. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement