与Listview不同,RecillerView没有附带onItemClickListener.因此,我们使用addOnItemTouchListener
binding.recyclerView1.addOnItemTouchListener(object: RecyclerView.OnItemTouchListener {
// This method is called when a touch event occurs on the RecyclerView ie click, scroll, long click etc, allowing the listener to intercept the event before it reaches the child views.
// If you return true from this method, you indicate that the touch event has been consumed and should not be further processed by child views or other touch listeners.
override fun onInterceptTouchEvent(rv: RecyclerView, e: MotionEvent): Boolean {
// for getting click event, you can:
if (e.action == MotionEvent.ACTION_UP) {
// you firebase code
}
return false
}
// allows you to handle touch events directly on the RecyclerView level. You might use this method for more global touch event handling, such as scrolling or zooming.
override fun onTouchEvent(rv: RecyclerView, e: MotionEvent) {
}
// called when a parent view requests that the RecyclerView should not intercept touch events.
override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {
}
})