0

I have created a new view called RecorderView and inside this class there is an onTouchEvent() like below,

@Override
    public boolean onTouchEvent(MotionEvent event) {

        if (isRecording) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                Log.d("JM", "ACTION_DOWN");
                return false;
            }

            if (event.getAction() == MotionEvent.ACTION_UP) {
                Log.d("JM", "ACTION_UP");
                handler.removeCallbacks(runnable);
                stopRecord();
            }
            return true;
        }
        return false;

    }

Then this view is inflated in a listview by extending BaseAdapter and there it has a setOnLongClickListener() inside getView() like below

holder.recordView.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                Log.d("JM", "On list item clicked");
                holder.recordView.startRecord();
                return false;
            }
        });

and now the problem is setOnLongClickListener() is not executing.

I am trying to sort it out for the last 2 days. But still not solved.

Jin
  • 462
  • 1
  • 7
  • 22

2 Answers2

1

Have you tried using OnItemLongClick to achieve what you want? I believe you want every item of the ListView to respond to OnLongClick, right?

Boris Mocialov
  • 3,439
  • 2
  • 28
  • 55
  • OnItemLongClick is calling but now MotionEvent.ACTION_UP is not getting – Jin Jul 18 '14 at 10:44
  • @JMIndia You have to return `true` in your `MotionEvent.ACTION_DOWN` so that `MOTIONEVENT.ACTION_UP` will be handled, otherwise control is not returned to the application and execution ends at `MotionEvent.ACTION_DOWN` event. You can test this by writing out some string to the console both for `MotionEvent.ACTION_DOWN` and `MotionEvent.ACTION_UP` – Boris Mocialov Jul 18 '14 at 10:59
  • Hi i tried that too. But at that time OnItemLongClick is not calling :( – Jin Jul 18 '14 at 11:08
0

When you override onTouchEvent for any view, then longpressed is consumed by onTouchEvent only. Here is one useful post, using solution mentioned you can implement long press using onTouchEvent.

textView.setOnTouchListener(new View.OnTouchListener() {

    private static final int MIN_CLICK_DURATION = 1000;
    private long startClickTime;

    @Override
    public boolean onTouch(View v, MotionEvent event) {

        switch (event.getAction()) {
        case MotionEvent.ACTION_UP:
            longClickActive = false;
            break;
        case MotionEvent.ACTION_DOWN:
            if (longClickActive == false) {
                longClickActive = true;
                startClickTime = Calendar.getInstance().getTimeInMillis();
            }
            break;
        case MotionEvent.ACTION_MOVE:
            if (longClickActive == true) {
                long clickDuration = Calendar.getInstance().getTimeInMillis() - startClickTime;
                if (clickDuration >= MIN_CLICK_DURATION) {
                    Toast.makeText(MainActivity.this, "LONG PRESSED!",Toast.LENGTH_SHORT).show();
                    longClickActive = false;
                }
            }
            break;
        }
        return true;
    }
});

in which private boolean longClickActive = false; is a class variable.

Community
  • 1
  • 1
Rajen Raiyarela
  • 5,526
  • 4
  • 21
  • 41