I am trying do I return the result of the position from my Recyclerview Adapter, but I can’t call the " adapter.setOnItemClickListener(this)" from MainActivity.kt? I get the error "Unresolved reference: setOnItemClickListener"?
I’ve had to post a new thread as I can’t get all the code to be shown – after taking quite a few hours failing on this I am losing the will to live 🙁
Adapter.kt
class UsersAdapter(
private val users: ArrayList<User>
) : RecyclerView.Adapter<UsersAdapter.DataViewHolder>() {
class DataViewHolder(itemView: View) :
RecyclerView.ViewHolder(itemView) {
fun bind(user: User) {
itemView.textViewUserName.text = user.name
Glide.with(itemView.imageViewAvatar.context)
.load(user.avatar)
.into(itemView.imageViewAvatar)
}
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int) =
DataViewHolder(
LayoutInflater.from(parent.context).inflate(
R.layout.item_layout, parent, false)
)
override fun getItemCount(): Int = Int.MAX_VALUE
override fun onBindViewHolder(holder: DataViewHolder, position: Int){
val pos = position % users.size
holder.bind(users[pos])
Log.d(Constraints.TAG, "onBindViewHolder:" + pos)
}
lateinit var listener: OnItemClickListener
public interface OnItemClickListener {
fun getAdapterPosition(position : Int )
}
public fun setOnItemClickListener(listener: OnItemClickListener) {
this.listener= listener
}
MainActivity.kt
class MainActivity : AppCompatActivity() {
lateinit var adapter: ConcatAdapter
lateinit var userVerticalAdapter: UsersAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupDataInRecyclerView()
}
//override
public fun getAdapterPosition(position : Int ){
// required value is in the position variable
Log.d(Constraints.TAG, "This is where it is going!" )
}
private fun setupDataInRecyclerView() {
recyclerView.layoutManager = LinearLayoutManager(this,
LinearLayoutManager.HORIZONTAL, false)
userVerticalAdapter = UsersAdapter(DataSource.getUser())
val listOfAdapters = listOf(userVerticalAdapter)
adapter = ConcatAdapter(listOfAdapters)
//This errors with "Unresolved reference: setOnItemClickListener"
adapter.setOnItemClickListener(this)
recyclerView.adapter = adapter
recyclerView.scrollToPosition(Int.MAX_VALUE/2)
ItemSnapHelper().attachToRecyclerView(recyclerView)
}
public fun ShowWhatRecieved(isitthere: Int){
ShowMeIt.text = isitthere.toString()}
}
Revised and Updated MainActvity.kt
class MainActivity : AppCompatActivity(), UsersAdapter.OnItemClickListener {
lateinit var adapter: ConcatAdapter
lateinit var userVerticalAdapter: UsersAdapter
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
setupDataInRecyclerView()
}
override public fun getAdapterPosition(position : Int ){
Log.d(Constraints.TAG, "This is where it is going!" )
}
private fun setupDataInRecyclerView() {
recyclerView.layoutManager = LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false)
userVerticalAdapter = UsersAdapter(DataSource.getUser())
val listOfAdapters = listOf(userVerticalAdapter)
adapter = ConcatAdapter(listOfAdapters)
adapter.setOnItemClickListener(this)
recyclerView.adapter = adapter
recyclerView.scrollToPosition(Int.MAX_VALUE/2)
ItemSnapHelper().attachToRecyclerView(recyclerView)
}
public fun ShowWhatRecieved(isitthere: Int){
ShowMeIt.text = isitthere.toString()
}
}
4
Answers
You are passing
this
as argument it will pass the context but in the parameter of functionsetOnItemClickListener
in adapter you have defined listener ofOnItemClickListener
type. So you need to pass the argument ofOnItemClickListener
type.You should implement the
OnItemClickListener
at your activity and you will be able to usethis
keyword. But you also can make an anonymous implementation ofOnItemClickListener
interface when you try to send.But I would prefer the option 1. To implement the
OnItemClickListener
to your activity, implement all methods of that interface and than you can usethis
keyword when you need to passOnItemClickListener
anywhere in that class.You have not implemented interface in your as below –
Here is the problem mate.
Despite all your effort by creating an interface and implementing it inside your recycler-view, I suggest you use lambda instead of interface since you are writing Kotlin, and writing a lambda instead of an interface is a cleaner approach in Kotlin for ClickListener.
Create a lambda parameter both for your adapter and your viewholder. since you want to pass the position of a clicked item so your lambda must have an
Int
input and nothing as output so it would look like this:listener: (Int) -> Unit
.The rest of your code would be something like this, you get the idea I just edited important parts:
then in your activity you can write:
or:
also since you’re using
ConcatAdapter
it’s better to usebindingAdapterPosition
instead ofadapterPosition
.