我想在recyclerview项目上实现onClickListener。这是我的 CustomAdapter.kt 代码。
import pygame
import os
import sys
pygame.init()
screen = pygame.display.set_mode((448, 576))
done = False
y = 320
x = 216
clock = pygame.time.Clock()
PACMANSPRITE = pygame.image.load("pacman.png").convert_alpha()
class pacman(pygame.sprite.Sprite):
def __init__(self, image, x, y):
self.image = image
self.y=y
self.x=x
screen.blit((image), (x,y))
def movement(self):
pressed= pygame.key.get_pressed()
if pressed[pygame.K_UP]:
self.y=y-10
if pressed[pygame.K_DOWN]:
self.y=y+10
if pressed[pygame.K_LEFT]:
self.x=x-10
if pressed[pygame.K_RIGHT]:
self.x=x+10
screen.blit(image, (x,y))
while not done:
for event in pygame.event.get():
if event.type == pygame.QUIT:
done = True
pygame.quit()
sys.exit()
screen.fill((0, 0, 0))
pacman(PACMANSPRITE, x ,y)
pacman.movement()
pygame.display.flip()
clock.tick(60)
我想从我的活动中点击监听器回调。
答案 0 :(得分:5)
class CustomAdapter(
val userList: List<User>,
val listener: (User) -> Unit
) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder = ViewHolder(LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false))
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.bindItems(userList[position])
}
override fun getItemCount(): Int = userList.size
inner class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
private val textViewName: TextView = itemView.findViewById(R.id.textViewUsername)
private val textViewAddress: TextView = itemView.findViewById(R.id.textViewAddress)
fun bindItems(user: User) = with(itemView) {
textViewName.text = user.name
textViewAddress.text = user.address
setOnClickListener { listener(user) }
}
}
}
然后
val adapter = CustomAdapter(userList) { user ->
// do something with user on click
}
recyclerView.adapter = adapter
答案 1 :(得分:0)
好的,让我们分3步完成
1-您使用onClick方法定义一个接口作为回调 像这样
for (DocumentFile file : pickedDir.listFiles()) {
if (file.isDirectory() && file.getName().equals("Portfolio")) {
file.createDirectory("Subdirectory");
}
}
在适配器中的2-秒在视图持有者中添加一个单击侦听器,并调用这样的接口方法
修改强>
interface UserClickCallbacks {
fun onUserClick(user: User)
}
3-在您的活动中,您实施了class CustomAdapter(val userList: ArrayList<User>, private val userClickCallbacks: UserClickCallbacks) : RecyclerView.Adapter<CustomAdapter.ViewHolder>() {
//this method is returning the view for each item in the list
class MyAdapter(val userList: ArrayList<User>)
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): CustomAdapter.ViewHolder {
val v = LayoutInflater.from(parent.context).inflate(R.layout.row_layout, parent, false)
return ViewHolder(v)
}
//this method is binding the data on the list
override fun onBindViewHolder(holder: CustomAdapter.ViewHolder, position: Int) {
holder.textViewName.text = user.name
holder.textViewAddress.text = user.address
}
//this method is giving the size of the list
override fun getItemCount(): Int {
return userList.size
}
//the class is hodling the list view
class ViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView), View.OnClickListener {
init {
view.setOnClickListener(this)
}
val textViewName = itemView.findViewById(R.id.textViewUsername)
val textViewAddress = itemView.findViewById(R.id.textViewAddress)
override fun onClick(p0: View?) {
userClickCallbacks.onUserClick(userList[adapterPosition])
}
}
}
界面
并覆盖像这样的UserClickCallbacks
方法
onUserClick()
答案 2 :(得分:0)
不要忘记将inner
添加到ViewHolder
课程,否则您将无法访问位于listener
内的Adapter
对象:
package com.niel.customview
import android.support.v7.widget.RecyclerView
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.ImageView
/**
* Created by nileshdeokar on 12/03/2018.
*/
class MediaAdapter(private var mData: ArrayList<String>) : RecyclerView.Adapter<MediaAdapter.ViewHolder>() {
lateinit var listener: OnItemClickedListener
override fun onCreateViewHolder(parent: ViewGroup?, viewType: Int): ViewHolder {
val view = LayoutInflater.from(parent?.context)
.inflate(R.layout.row_media, parent, false)
return ViewHolder(view)
}
override fun onBindViewHolder(holder: ViewHolder?, position: Int) {
val model = mData[position]
}
override fun getItemCount(): Int {
return mData.size
}
inner class ViewHolder : RecyclerView.ViewHolder, View.OnClickListener {
var imageViewSq: ImageView
constructor(itemView: View?) : super(itemView) {
imageViewSq = itemView?.findViewById(R.id.imgSq)!!
itemView.setOnClickListener(this)
}
override fun onClick(p0: View?) {
listener.onItemClick(adapterPosition)
}
}
fun setOnItemClickedListener(listener: OnItemClickedListener) {
this.listener = listener
}
interface OnItemClickedListener {
fun onItemClick(position: Int)
}
}