Jin
Jin

Reputation: 472

In android, onlongclick is not calling after ontouchevent

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.

Upvotes: 0

Views: 415

Answers (2)

Rajen Raiyarela
Rajen Raiyarela

Reputation: 5636

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.

Upvotes: 0

Boris Mocialov
Boris Mocialov

Reputation: 3489

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

Upvotes: 1

Related Questions