Question

I have a listview with custom adapter. Each row has some text and a clickable FrameLayout, that has onClickListener set on it. Rows can be deleted with swipe (like gmail or hangouts). I am using Roman Nurik's SwipeToDismiss library. The problem is that the clickable FrameLayout takes half of space of row, so it is very hard to swipe the row away, because it consumes the MOVE event.

I have read that I can pass event to the parent view with extending FrameLayout and overriding the dispatchTouchEvent method. I have tried that, but then all events are passed and therefore FrameLayout is no longer clickable.

I also tried this:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
       return false;
    }
    return super.dispatchTouchEvent(ev);
}

but it looks like once the view "got" the DOWN event, it won't pass it to parent anymore.

My question: What should I do, to keep onClick event on the current view, but pass MOVE events to the parent view?

Was it helpful?

Solution

I got it working. I extended FrameLayout and overrided method dispathTouchEvent:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (ev.getAction() == MotionEvent.ACTION_MOVE) {
        //if move event occurs
        long downTime = SystemClock.uptimeMillis();
        long eventTime = SystemClock.uptimeMillis() + 100;
        float x = ev.getRawX();
        float y = ev.getRawY();
        int metaState = 0;
        //create new motion event
        MotionEvent motionEvent = MotionEvent.obtain(
                downTime, 
                eventTime, 
                MotionEvent.ACTION_DOWN, 
                x, 
                y, 
                metaState
        );
        //I store a reference to listview in this layout
        listview.dispatchTouchEvent(motionEvent); //send event to listview
        return true;
    }
    return super.dispatchTouchEvent(ev);
}

Now the FrameLayout is clickable, but after MOVE happens, it creates a new MotionEvent and sends it to ListView, where the content is scrolled or swiped away. I should probably add some +- to MOVE event, because when you press with finger, you usually do some MOVE events with it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top