Home > Enterprise >  Please tell me how to transfer the ID (position) of the view element on recyclerview to another clas
Please tell me how to transfer the ID (position) of the view element on recyclerview to another clas

Time:12-24

Please tell me how to transfer the ID (position) of the view element on recyclerview to another class?

class CardAdapter : RecyclerView.Adapter<CardAdapter.CardViewHolder>(), View.OnClickListener {
private var cardList = ArrayList<Card>()
private lateinit var card: Card

class CardViewHolder(
    val binding: FragmentCardBinding
) : RecyclerView.ViewHolder(binding.root)

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CardViewHolder {
    val inflater = LayoutInflater.from(parent.context)
    val binding = FragmentCardBinding.inflate(inflater, parent, false)
    return CardViewHolder(binding)
}

@SuppressLint("SetTextI18n")
override fun onBindViewHolder(holder: CardViewHolder, position: Int) {
    card = cardList[position]
    ..
}

override fun getItemCount(): Int = cardList.size

override fun onClick(v: View) {
    when (v.id) {
        R.id.root_card_template -> {
            val intent = Intent(v.context, ProductActivity::class.java)
            // need put id into ProductActivty
            intent.putExtra("item", card.id)
            v.context.startActivity(intent)

        }
    }
}

At the moment, it only passes the ID of the last generated element. For some reason there is almost no information on the Internet on this score

CodePudding user response:

first you need to creat an interface

interface OnItemListener {
    fun onItemSelect(position: Int)
}

then in your class that calls the recyclerview adapter, pass it to your recyclerView Adapter like this

 var cardAdapter = CardAdapter(object :
            OnItemListener {
            override fun onItemSelect(position: Int) {
               // you can handle your data here
               // your position that you passed comes here
            }
        })
 var layoutManager = GridLayoutManager(context, 2)
 yourRecyclerViewId.adapter = cardAdapter
 yourRecyclerViewId.layoutManager = layoutManager

finally in your adapter do like this

class CardAdapter(
private val onItemListener: OnItemListener
) 
: RecyclerView.Adapter<CardAdapter.CardViewHolder>(), View.OnClickListener {
        private var cardList = ArrayList<Card>()
        private lateinit var card: Card

and in your on click event in the adapter call it as below:

        onItemListener.onItemSelect(yourPosition)

CodePudding user response:

Just create an interface and implement that in the calling activity. While creating an instance of your adapter inside the activity, pass that interface along and on the click event of the view in the adapter class, call the interface's method with the data that you want to pass back to the activity.

 interface OnItemClickListener{
        fun onClick(pos: Int)
    }



 class YourActivity: AppCompatActivity(), OnItemClickListener {

    override fun onStart() {
        super.onStart()
        //Create an instance of your adapter and pass the interface.
        // Here @this context is being passed as Activity is implementing the interface.
        val cardListAdapter = CardListAdapter(this)

    }
    override fun onClick(pos: Int) {
        //Add your logic
    }

}

// Then in your adapter class

class YourAdapter(private val itemClickListener: OnItemClickListener) :
    RecyclerView.Adapter<CardListAdapter.CardViewHolder>() {
        //Your code
        override fun onBindViewHolder(holder: CardListAdapter.CardViewHolder, position: Int) {
            yourView.setOnClickListener {
                itemClickListener.onClick(position)
            }
        }
        
    }
  • Related