Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
562 views
in Technique[技术] by (71.8m points)

event handling - onTouch, onLongClick together in android

I'am adding imageviews to parent layout dynamically. And i'am performing zoom in/out operations onTouch of added image. I want to remove the added view onLongPress of it.

img.setOnLongClickListener(longClickAction);
img.setOnTouchListener(touchAction); 

onLongPress :

OnLongClickListener longClickAction = new OnLongClickListener() {

    @Override
    public boolean onLongClick(View v) {

        parentLayout.removeView((ImageView)v);
        return false;
    }
};

onTouch :

OnTouchListener touchAction = new OnTouchListener() {

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        ImageView i = (ImageView)v;

        //perfrom zoom operation on touch of imageview
        zoom(i, event);
        return true; 

    }
};

Only Touch events are working. Why? How can i have both? Where iam going wrong? What should i do to remove added view? Please help me. Thanks in advance.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

onTouch is always called for your view since this is the initial state of dispatching the events to the view. When you long press your view this still calls onTouch first and since you return true in onTouch(which means that you've consumed this event and it should not be further dispatched) you won't get onLongPress called. What will do the trick is returning false in onTouch


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...