TheLettuceMaster
TheLettuceMaster

Reputation: 15734

OnGestureListener cancelling out onCreateContextMenu

I have an OnGestureListener on my ListView because I need gestures over it, but now ContextMenu will not work.

Edited code:

I have this inside the onActivityCreated method of my Fragment for the ListView:

gestureDetector = new GestureDetector(getActivity(),
            new GestureListener());
    View.OnTouchListener gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    };
    listView.setOnTouchListener(gestureListener);

Doing this allow my gestures but, commenting it out brings back my ConextMenu.

I know I can possibly use showContextMenu(v), but not sure where to put it if that is the answer (I have tried in a few places).

Inside my Fragment where adapter is set, I have this empty method,

public void onLongPress(MotionEvent e) {

    return;
}

I tried putting that code in there but there is no reference to a View. So not sure how to proceed?

Upvotes: 0

Views: 323

Answers (1)

user
user

Reputation: 87064

You could still use the GestureDetector to "see" the fling gesture and also have the ContextMenu opened when long clicking on the ListView using the code below in the onLongPress() callback:

@Override
public void onLongPress(MotionEvent e) {
    registerForContextMenu(listView);
    final int x = (int) e.getX();
    final int y = (int) e.getY();
    int position = listView.pointToPosition(x, y);
    final int firstVisible = listView.getFirstVisiblePosition();
    View v = listView.getChildAt(position - firstVisible);
    getActivity().openContextMenu(v);
}

A complete Fragment class example can be found here.

Upvotes: 1

Related Questions