如何在recyclerView中管理点击事件
Posted
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了如何在recyclerView中管理点击事件相关的知识,希望对你有一定的参考价值。
我正在使用Recycler View列出项目的地方创建联系人应用程序。回收者视图工作正常,并列出了所有物品,如下图所示
现在,我希望每个联系人单击将其详细信息转移到另一个如下所示的布局(配置文件布局)中>]
回收站适配器在onclick上工作,即使只是制作吐司,但对Dialog不能将数据传输到另一个布局都无效。我尝试了几次尝试,但都证明是徒劳的。
这是我的适配器package com.example.newcontactapp
import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
class ContactAdapter (data:ArrayList<ContactClass>, internal var context: Context):RecyclerView.Adapter<ContactAdapter.ContactViewHolder>()
internal var data : ArrayList<ContactClass>
init
this.data = data
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder
val layout = LayoutInflater.from(context).inflate(R.layout.contact_row, parent, false)
return ContactViewHolder(layout)
override fun onBindViewHolder(holder:ContactViewHolder, position:Int)
holder.name.text = data[position].name
holder.phoneNumber.text = data[position].phone
holder.image.setImageResource(data[position].image)
holder.card.setOnClickListener
Toast.makeText(context, data[position].name, Toast.LENGTH_SHORT).show()
// val profilePage = Dialog(this.context)
// profilePage.setContentView(R.layout.profile)
//// profilePage.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
// profilePage.setTitle("Profile page")
//
// val profileName = profilePage.findViewById<TextView>(R.id.profileName)
// val profileEmail = profilePage.findViewById<TextView>(R.id.profileEmail)
// val profileImage = profilePage.findViewById<ImageView>(R.id.profileImage)
//
// profileName.text = data[position].name
// profileEmail.text = data[position].phone
// profileImage.setImageResource(data[position].image)
//
// profilePage.show()
override fun getItemCount(): Int
return data.size
class ContactViewHolder(contactView: View): RecyclerView.ViewHolder(contactView)
internal var name: TextView
internal var image: ImageView
internal var phoneNumber: TextView
internal var card: ViewGroup;
init
name = itemView.findViewById(R.id.itemText)
image = itemView.findViewById(R.id.itemImage)
phoneNumber = itemView.findViewById(R.id.itemPhone)
card = itemView.findViewById(R.id.contactCard)
package com.example.newcontactapp
import android.app.Dialog
import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import android.widget.Toast
import androidx.recyclerview.widget.RecyclerView
class ContactAdapter (data:ArrayList<ContactClass>, internal var context: Context):RecyclerView.Adapter<ContactAdapter.ContactViewHolder>()
internal var data : ArrayList<ContactClass>
init
this.data = data
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder
val layout = LayoutInflater.from(context).inflate(R.layout.contact_row, parent, false)
return ContactViewHolder(layout)
override fun onBindViewHolder(holder:ContactViewHolder, position:Int)
holder.name.text = data[position].name
holder.phoneNumber.text = data[position].phone
holder.image.setImageResource(data[position].image)
holder.card.setOnClickListener
Toast.makeText(context, data[position].name, Toast.LENGTH_SHORT).show()
// val profilePage = Dialog(this.context)
// profilePage.setContentView(R.layout.profile)
//// profilePage.window?.setType(WindowManager.LayoutParams.TYPE_APPLICATION_PANEL)
// profilePage.setTitle("Profile page")
//
// val profileName = profilePage.findViewById<TextView>(R.id.profileName)
// val profileEmail = profilePage.findViewById<TextView>(R.id.profileEmail)
// val profileImage = profilePage.findViewById<ImageView>(R.id.profileImage)
//
// profileName.text = data[position].name
// profileEmail.text = data[position].phone
// profileImage.setImageResource(data[position].image)
//
// profilePage.show()
override fun getItemCount(): Int
return data.size
class ContactViewHolder(contactView: View): RecyclerView.ViewHolder(contactView)
internal var name: TextView
internal var image: ImageView
internal var phoneNumber: TextView
internal var card: ViewGroup;
init
name = itemView.findViewById(R.id.itemText)
image = itemView.findViewById(R.id.itemImage)
phoneNumber = itemView.findViewById(R.id.itemPhone)
card = itemView.findViewById(R.id.contactCard)
MainActivitypackage com.example.newcontactapp
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// setSupportActionBar(toolbar)
val contacts = ArrayList<ContactClass>()
contacts.add(ContactClass("Darot", R.drawable.maleavatar, "080000000000"))
contacts.add(ContactClass("Lewis", R.drawable.maleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
val adapter = ContactAdapter(contacts, applicationContext)
recycler.layoutManager = LinearLayoutManager(applicationContext)
recycler.adapter = adapter
fab.setOnClickListener view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
override fun onCreateOptionsMenu(menu: Menu): Boolean
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
override fun onOptionsItemSelected(item: MenuItem): Boolean
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId)
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
package com.example.newcontactapp
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import androidx.appcompat.app.AppCompatActivity
import android.view.Menu
import android.view.MenuItem
import androidx.recyclerview.widget.LinearLayoutManager
import kotlinx.android.synthetic.main.activity_main.*
class MainActivity : AppCompatActivity()
override fun onCreate(savedInstanceState: Bundle?)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
// setSupportActionBar(toolbar)
val contacts = ArrayList<ContactClass>()
contacts.add(ContactClass("Darot", R.drawable.maleavatar, "080000000000"))
contacts.add(ContactClass("Lewis", R.drawable.maleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
contacts.add(ContactClass("Chigozie", R.drawable.femaleavatar, "080000000000"))
val adapter = ContactAdapter(contacts, applicationContext)
recycler.layoutManager = LinearLayoutManager(applicationContext)
recycler.adapter = adapter
fab.setOnClickListener view ->
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show()
override fun onCreateOptionsMenu(menu: Menu): Boolean
// Inflate the menu; this adds items to the action bar if it is present.
menuInflater.inflate(R.menu.menu_main, menu)
return true
override fun onOptionsItemSelected(item: MenuItem): Boolean
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId)
R.id.action_settings -> true
else -> super.onOptionsItemSelected(item)
我想使用Dialog方法,因为我觉得它比较容易,但是我无法添加窗口错误,我尝试引入一个Activity,但是事实证明很困难。请帮助。我将其用作学习项目。
我正在使用Recycler View列出项目的地方创建联系人应用程序。回收者视图工作正常,并列出了如下图所示的物品。现在,我希望每个物品...
答案
在适配器和活动之间进行通信的最佳方式使用接口。
以上是关于如何在recyclerView中管理点击事件的主要内容,如果未能解决你的问题,请参考以下文章
Recyclerview点击事件,更新item的UI+更新Recyclerview外的控件
Android:RecyclerView中添加Item点击事件
如何在recyclerview android中的single_row_item的每个视图(imageView,textView)上添加不同的点击事件?