android 请告诉我如何将recyclerview上的view元素的ID(位置)转移到另一个类

juzqafwq  于 2022-12-25  发布在  Android
关注(0)|答案(2)|浏览(105)

请告诉我如何将recyclerview上的view元素的ID(位置)转移到另一个类中?

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)

        }
    }
}

目前,它只传递最后生成的元素的ID,由于某种原因,Internet上几乎没有关于这个分数的信息

68de4m5k

68de4m5k1#

只需创建一个接口并在调用Activity中实现它。在Activity中创建适配器示例时,传递该接口沿着并在适配器类中的视图的单击事件上,使用要传递回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)
            }
        }
        
    }
agxfikkp

agxfikkp2#

首先你需要创建一个接口

interface OnItemListener {
    fun onItemSelect(position: Int)
}

然后在调用recyclerView适配器的类中,将其传递给recyclerView适配器,如下所示

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

最后在你的适配器这样做

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

并在适配器的on click事件中调用它,如下所示:

onItemListener.onItemSelect(yourPosition)

相关问题