onTouch, onLongClick together in android

徘徊边缘 提交于 2019-11-28 03:35:38

问题


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.


回答1:


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




回答2:


As discussed by @asenovm The onTouch() is always called as it is the initial state of dispatching the events to the view, but if we return value false in onTouch() Then both will work like a charm and the problem will be solved.

Edit: My suggestion to the users is that instead of implementing OnLongClickListener() and OnTouch() together, try using the function of OnLongClickListener() in Double click event.

You can implement Double Click in following ways:

int i = 0;
btn.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    i++;
    Handler handler = new Handler();
    Runnable r = new Runnable() {

        @Override
        public void run() {
            i = 0;
        }
    };

    if (i == 1) {
        //Single click
        handler.postDelayed(r, 250);
    } else if (i == 2) {
        //Double click
        i = 0;
        ShowDailog();
    }


  }
});



回答3:


Dynamically remove controls

may help you

using onTouch and onLongClick




回答4:


Try this, it works fine for me.

boolean isMoving= false;

yourView.setOnTouchListener(new View.OnTouchListener(){
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        if (event.getAction() == MotionEvent.ACTION_MOVE) {
            isMoving = true;
            Log.i("isMoving:", "true");
        }
        if(event.getAction()==MotionEvent.ACTION_UP){
            isMoving=false;
            Log.i("isMoving:","false");
        }
        return false;
    }
});
yourView.setOnLongClickListener(new View.OnLongClickListener() {
    @Override
    public boolean onLongClick(View v) {
        if(!isMoving){
            //do onlongclick event here
        }
        return true;
    }
});;


来源:https://stackoverflow.com/questions/10946751/ontouch-onlongclick-together-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!