Untitled

mail@pastecode.io avatar
unknown
plain_text
a year ago
2.1 kB
1
Indexable
Never
package com.example.storeapp.ui.features.mainfragment

import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import androidx.recyclerview.widget.RecyclerView.Adapter
import com.bumptech.glide.Glide
import com.example.storeapp.databinding.ProductsitemBinding
import com.example.storeapp.model.data.ProductLists

class RecyclreAdapterBestSellersMainFragment(
    val productarraylist: List<ProductLists.ProductslistItem>,
    val clickoneachproduct: clickonproduct
) : Adapter<RecyclreAdapterBestSellersMainFragment.myViewHolder>() {
    lateinit var binding: ProductsitemBinding

    inner class myViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
        fun binder(position: Int) {
            itemView.setOnClickListener {
            }
            binding.txtViewNameProductItem.text = productarraylist[position].name
            binding.txtViewPriceProductItem.text = productarraylist[position].price
            Glide.with(binding.root)
                .load(productarraylist[position].images[0].src)
                .into(binding.imgViewProductItem)
            binding.imgViewProductItem.setOnClickListener {
                clickoneachproduct.click(productarraylist[position], adapterPosition)
            }
        }
    }
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): myViewHolder {
        binding = ProductsitemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return myViewHolder(binding.root)
    }
    override fun getItemCount(): Int {
        return productarraylist.size
    }
    override fun onBindViewHolder(holder: myViewHolder, position: Int) {
        holder.binder(position)
    }
    override fun getItemViewType(position: Int): Int {
        //ghati nakardane recyclerview:khilimohem
        return position
    }
    override fun getItemId(position: Int): Long {
        return position.toLong()
    }
    interface clickonproduct {
        fun click(data: ProductLists.ProductslistItem, adapterPosition: Int)
    }
}