RecyclerView的自定义侦听器未检测到点击

时间:2018-07-11 03:07:49

标签: android android-recyclerview kotlin adapter onclicklistener

This  是我关注的教程,在“使用侦听器附加Click Handlers”下

我正在尝试实现自定义侦听器以传递给适配器。这是为了确保当单击RecyclerView项时,适配器运行MainActivity中的代码。 它是在adapter.setOnItemClickListener

内部完成的

当我在recyclerview中单击其中一项时,showAlertDialog()无法运行。 是因为我将recyclerview放在了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 个答案:

答案 0 :(得分:0)

在您的onBindViewHolder方法内添加以下代码。

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

    holder.view.isLongClickable = true

    //set the listener like below 
    holder.view.setOnClickListener(holder);
}

这应该可以。但是您在整个侦听器方面都有些复杂。可以用简单的方式实现。