Custom listener for RecyclerView is not detecting clicks

This is the tutorial I am following, under "Attaching Click Handlers using Listeners"

I am trying to implement a custom listener to pass to an adapter. This is to ensure the adapter runs the code in MainActivity when a RecyclerView item is clicked. It is done inside adapter.setOnItemClickListener

When I click on one of the items in my recyclerview, showAlertDialog() does not run. Is it because I have placed my recyclerview in a swiperefreshlayout?

class MainActivity : AppCompatActivity(), MainContract.View {

    private lateinit var presenter: MainContract.Presenter
    private lateinit var adapter: MainAdapter

    override fun onCreate(savedInstanceState: Bundle?) {

        ...

        swipe_container.setOnRefreshListener { presenter.refreshPlaceList() }

        recyclerViewPlaces.layoutManager = LinearLayoutManager(this)
    }

    override fun showFavouritePlaces(favouritePlaces: List<Place>) {
        adapter = MainAdapter(favouritePlaces.toMutableList(), this)

        // Anonymous object passed here as listener
        adapter.setOnItemClickListener(object: MainAdapter.OnItemClickListener{
            override fun onItemClick(itemView: View) {
                showAlertDialog()
            }
        })

        recyclerViewPlaces.adapter = adapter
    }

    override fun showFavouritePlace(favouritePlace: Place) {
        if (this::adapter.isInitialized) {
            adapter.addFavouritePlace(favouritePlace)
        } else {
            adapter = MainAdapter(mutableListOf(favouritePlace), this)
            recyclerViewPlaces.adapter = adapter
        }
    }

    override fun showAlertDialog() {
        val builder = AlertDialog.Builder(this)

        builder.setTitle("Delete")

        builder.setMessage("Would you like to delete this place?")

        builder.setPositiveButton("YES") { dialog, which ->
            Toast.makeText(applicationContext, "Yes selected", Toast.LENGTH_SHORT).show()
        }

        builder.setNegativeButton("No") { dialog, which ->
            Toast.makeText(applicationContext, "Nope", Toast.LENGTH_SHORT).show()
        }

        val dialog: AlertDialog = builder.create()

        dialog.show()
    }
}


class MainAdapter(private val favouritePlaces: MutableList<Place>, private val context: Context)
    : RecyclerView.Adapter<MainAdapter.CustomViewHolder>() {

    private lateinit var listener: OnItemClickListener

    interface OnItemClickListener {
        fun onItemClick(itemView: View)
    }

    fun setOnItemClickListener(listener: OnItemClickListener) {
        this.listener = listener
    }

    class CustomViewHolder(val view: View, private val listener: OnItemClickListener)
                                : RecyclerView.ViewHolder(view), View.OnClickListener {

        override fun onClick(v: View?) {
            if (listener != null) {
                listener.onItemClick(v!!)
            }
        }
    }

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MainAdapter.CustomViewHolder {
        val placeView = LayoutInflater.from(parent.context)
                .inflate(R.layout.place_list_item, parent, false)

        return MainAdapter.CustomViewHolder(placeView, listener)
    }

    override fun onBindViewHolder(holder: MainAdapter.CustomViewHolder, position: Int) {

        holder.view.isLongClickable = true

        // Setting XML properties
    }
}

1 answer

  • answered 2018-07-11 11:49 rafa

    Add below code inside your onBindViewHolder method.

    override fun onBindViewHolder(holder: MainAdapter.CustomViewHolder, position: Int) {
    
        holder.view.isLongClickable = true
    
        //set the listener like below 
        holder.view.setOnClickListener(holder);
    }
    

    This should work.But you have kind of complicated the whole listener thing.Can implemented in simple way.