2 years ago

#32499

test-img

jenik2205

How to get selected item in RecyclerView without using onClick method in Kotlin

I need to display some information in Activity from Adapter but without using onClick method - I want to get selected item at any time without click on the item (e.g. when the activity is open, first item is selected (different background color) and I need to show some data from this item. I have similiar question here How to display item info on selected item in RecyclerView using Kotlin but there is List as output of class. I have ListAdapter here in this example. I know I could use getLayoutManager().findFirstVisibleItemPosition() method in your RecyclerView or getAdapterPosition() at the Adapter but I don't know how.

class ExampleAdapter(var context: Context?, private val listener: OnItemClickListener): ListAdapter<ExampleTuple, ExampleAdapter.ItemViewHolder>(DiffCallback()) {
    private var selectedItemPosition: Int = 0
    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ItemViewHolder {
        val binding = RecyclerViewItemBinding.inflate(LayoutInflater.from(parent.context), parent, false)
        return ItemViewHolder(binding)
    }   

    //fun getCurrentItem(): ExampleTuple = currentList[selectedItemPosition]

    override fun onBindViewHolder(holder: ItemViewHolder, position: Int) {
        val currentItem = getItem(position)
        if (currentItem != null) {
            holder.bind(currentItem)
        }
        if (selectedItemPosition == position){
            holder.itemView.setBackgroundColor(Color.parseColor("#DA745A"))
        } else {
            holder.itemView.setBackgroundColor(Color.TRANSPARENT)
        }
    }
    inner class ItemViewHolder(private val binding: DokladyItemBinding): RecyclerView.ViewHolder(binding.root) {
        init {
            binding.root.setOnClickListener{
                val position = bindingAdapterPosition
                if (position != RecyclerView.NO_POSITION){
                    val item = getItem(position)
                    if (item != null){
                        listener.onItemClick(item)
                    }
                }
                notifyItemChanged(selectedItemPosition)
                selectedItemPosition = absoluteAdapterPosition
                notifyItemChanged(selectedItemPosition)
            }
            binding.root.setOnLongClickListener{
                val positionLong = bindingAdapterPosition
                if (positionLong != RecyclerView.NO_POSITION){
                    val itemLong = getItem(positionLong)
                    if (itemLong != null){
                        listener.onLongClick(itemLong)
                    }
                }
                true
            }
        }
        fun bind(exampleItem: ExampleTuple){
            binding.apply {
                tvDOKL.text = exampleItem.doklad.toString()
                tvORG.text = exampleItem.odj.toString()
                tvDATE.text = exampleItem.datuct.toString()
            }
        }
    }
    interface OnItemClickListener{
        fun onItemClick(exampleItem: ExampleTuple)
        fun onLongClick(exampleItem: ExampleTuple)
    }
    class DiffCallback: DiffUtil.ItemCallback<ExampleTuple>(){
        override fun areItemsTheSame(oldItem: ExampleTuple, newItem: ExampleTuple) =
            oldItem.doklad == newItem.doklad

        override fun areContentsTheSame(oldItem: ExampleTuple, newItem: ExampleTuple) =
            oldItem == newItem
    }
}

I use binding in Activity

binding.apply {
            recyclerView.apply {
                adapter = exampleAdapter
                layoutManager = LinearLayoutManager(this@MainActivity)
            }
            exampleViewModel.getAll(idExample).observe(this@MainActivity){
                dokladAdapter.submitList(it)   
            }
        }

android

kotlin

android-recyclerview

android-adapter

0 Answers

Your Answer

Accepted video resources