Detect click on RecyclerView outside of items

前端 未结 6 2416
醉话见心
醉话见心 2021-02-18 22:10

I have a RecyclerView with 2 items that don\'t fill the whole screen. How can I detect that the user clicked on the empty part of the RecyclerView (meaning clicked directly on t

6条回答
  •  北海茫月
    2021-02-18 22:52

    @Angel Kjoseski answer would look like this in Kotlin:

    yourRecyclerView.addOnItemTouchListener(object : RecyclerView.OnItemTouchListener {
                    override fun onInterceptTouchEvent(recyclerView: RecyclerView, motionEvent: MotionEvent): Boolean {
                        return when {
                            motionEvent.action != MotionEvent.ACTION_UP || recyclerView.findChildViewUnder(motionEvent.x, motionEvent.y) != null -> false
                            else -> {
                                // do something here
                                true
                            }
                        }
                    }
    
                    override fun onRequestDisallowInterceptTouchEvent(disallowIntercept: Boolean) {}
                    override fun onTouchEvent(recyclerView: RecyclerView, motionEvent: MotionEvent) {}
                })
    

提交回复
热议问题