我想使用Kotlin从RecyclerView开始一项新的活动。
我仍在探索Kotlin,目前停留在如何从RecyclerView中打开新活动的地方。
class HomeScreenRecyclerAdapter : RecyclerView.Adapter<HomeScreenRecyclerAdapter.ViewHolder>()
{
private val titles = arrayOf("About Me",
"About Me", "About Me", "About Me"
)
private val details = arrayOf("Item one details", "Item two details",
"Item three details", "Item four details")
private val images = intArrayOf(R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background)
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val v = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.main_card_view, viewGroup, false)
return ViewHolder(v)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.itemTitle.text = titles[position]
viewHolder.itemDetail.text = details[position]
viewHolder.itemImage.setImageResource(images[position])
}
override fun getItemCount(): Int {
return titles.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val intent: Intent? = null
var itemImage: ImageView
var itemTitle: TextView
var itemDetail: TextView
init {
itemImage = itemView.findViewById(R.id.main_image_view)
itemTitle = itemView.findViewById(R.id.main_title_view)
itemDetail = itemView.findViewById(R.id.main_description_view)
itemView.setOnClickListener {
}
}
}
}
我只是想不出如何为RecyclerView中的每个项目启动一个新活动。我知道我正在使事情变得更复杂。
答案 0 :(得分:0)
只需尝试以下解决方案: 首先,在您的适配器中,将监听器写入您的recyclerview项目:
class HomeScreenRecyclerAdapter(val data: ArrayList<Data>) : RecyclerView.Adapter<ContactAdapter.ViewHolder>() {
private var listener: ((Data) -> Unit)? = null
...
fun setOnItemClickListener(f: (Data) -> Unit) {
listener = f
}
在init里面:
itemView.setOnClickListener {
listener?.invoke(data[adapterPosition])
}
最后,在“活动”中,您可以使用手柄项clickListeners:
private val data = ArrayList<Data>()
val adapter = HomeScreenRecyclerAdapter(data)
adapter.setOnItemClickListener {
when(it){
1->{startActivity(Intent(context, YourActivity1::class.java))}
2->{startActivity(Intent(context, YourActivity2::class.java))}
...
else->{}
}
}
数据是您商品的模型类。 仅此而已,希望对您有所帮助!
答案 1 :(得分:-1)
只需在ViewHolder itemOnClick方法内启动活动,如下所示。并且您需要传递适配器活动的上下文引用。
class HomeScreenRecyclerAdapter(var mContext:Context) : RecyclerView.Adapter<HomeScreenRecyclerAdapter.ViewHolder>()
{
private val titles = arrayOf("About Me",
"About Me", "About Me", "About Me")
private val details = arrayOf("Item one details", "Item two details",
"Item three details", "Item four details")
private val images = intArrayOf(R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background, R.drawable.ic_launcher_background,
R.drawable.ic_launcher_background)
override fun onCreateViewHolder(viewGroup: ViewGroup, i: Int): ViewHolder {
val v = LayoutInflater.from(viewGroup.context)
.inflate(R.layout.main_card_view, viewGroup, false)
return ViewHolder(v)
}
override fun onBindViewHolder(viewHolder: ViewHolder, position: Int) {
viewHolder.itemTitle.text = titles[position]
viewHolder.itemDetail.text = details[position]
viewHolder.itemImage.setImageResource(images[position])
}
override fun getItemCount(): Int {
return titles.size
}
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val intent: Intent? = null
var itemImage: ImageView
var itemTitle: TextView
var itemDetail: TextView
init {
itemImage = itemView.findViewById(R.id.main_image_view)
itemTitle = itemView.findViewById(R.id.main_title_view)
itemDetail = itemView.findViewById(R.id.main_description_view)
itemView.setOnClickListener {
mContext.startActivity(Intent(mContext, ActivityNameWhichYouWantCall::class.java))
}
}
}
}
答案 2 :(得分:-1)
只需使用此:
context.startActivity(Intent(context, Activity::class.java))