Mahoni
Mahoni

Reputation: 7466

Perform work in an on touch listener and then delegate it to the previous one

In a ListView item I want to set up a touch listener, generally speaking I just want to perform some actions and then again delegate the touch event to the previous touch listener.

Something like:

 convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // do something else
                convertView.dispatchTouchEvent(event);

                return true;
            }
        });

does not work, since it leads to a StackOverflow. What can I do here?

Upvotes: 1

Views: 126

Answers (1)

Gilad Haimov
Gilad Haimov

Reputation: 5867

The way to delegate touch event for "others" to handle is to mark it as unconsumed

You do it by having your handler return false:

convertView.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                // do your staff else

                // do not call dispatchTouchEvent(event)

                return false; <------------ inform Android event was not consumed
            }
        });

This will allow you to add your delta of processing ("do your staff") and pass it on, possibly to its container view, for further processing.

Upvotes: 1

Related Questions