Android RecyclerView Empty Space Click Event?

Asked 2 years ago, Updated 2 years ago, 33 views

Not an event when you click RecyclerView item

I wanted to catch an event when I clicked on the RecyclerView empty space (We took RecyclerView as a whole)

I tried setOnClickListener on RecyclerView, but I can't catch the event when I click on the empty space

Can't I get an event when I click on the empty space instead of the RecyclerView item?

I wrapped it in a linear layout and tried setOnClickListener in that linear layout In order to receive a click event in the linear layout, there must be no view inside the linear.

For your information, we also receive and process RecyclerView item click events and long click events

Thank you^

^

android

2022-09-22 13:27

1 Answers

You can think of click events as basic features that View supports. RecyclerView extends ViewGroup and ViewGroup extends View. In conclusion, since the click event is processed in the View class, the touch event must be passed to the top class, View, before the click event can be used.

Looking at the RecyclerView code, onInterceptTouchEvent and onTouchEvent processing touch events to scroll and onItemTouchListener and do not make superclass method calls. Therefore, onClick is not invoked when setOnClickListener is set because the click event itself does not occur.

It is not common to register a click event in RecyclerView itself, but I think we can handle it as follows.

class TestRecyclerView (context: Context?, attrs: AttributeSet?): RecyclerView (context, attrs) {

    private var clickTriggerRunnable: ClickTriggerRunnable? = null
    private val touchSlop: Int = ViewConfiguration.get(context).scaledTouchSlop
    private var touchStartPointX = 0.0f
    private var touchStartPointY = 0.0f

    override fun onTouchEvent(e: MotionEvent?): Boolean {
        e?.run {
            when (action) {
                MotionEvent.ACTION_DOWN -> {
                    touchStartPointX = x
                    touchStartPointY = y

                    //Run Runnable for Click Event Processing
                    clickTriggerRunnable = ClickTriggerRunnable()
                    handler.postDelayed(clickTriggerRunnable, ViewConfiguration.getTapTimeout().toLong())
                }
                MotionEvent.ACTION_MOVE -> {
                    val deltaX = Math.abs(touchStartPointX - x)
                    val deltaY = Math.abs(touchStartPointY - y)

                    //Cancel callback if out of touch area for click
                    clickTriggerRunnable?.let {
                        if (deltaX > touchSlop || deltaY > touchSlop) {
                            handler.removeCallbacks(clickTriggerRunnable)
                            clickTriggetRunnable = null
                        }
                    }
                }
                MotionEvent.ACTION_UP -> {
                    clickTriggerRunnable?.let {
                        val triggered = it.isClickTriggered
                        handler.removeCallbacks(it)
                        clickTriggerRunnable = null

                        return when {
                            triggered -> {
                                //Implement desired click action here
                                true
                            }
                            else -> super.onTouchEvent(this)
                        }
                    }
                }
                else -> super.onTouchEvent(e)
            }
        }
        return super.onTouchEvent(e)
    }

    inner class ClickTriggerRunnable : Runnable {

        var isClickTriggered = false

        override fun run() {
            isClickTriggered = true
        }
    }
}


2022-09-22 13:27

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.