android setOnLongClickListner does not work with onTouch event

混江龙づ霸主 提交于 2019-12-14 01:55:12

问题


i have a image view which is drag able and zoom able but now i also need to put setOnLongClickListner on my image view. i have done that but it is not working. but when i disabled the ontouch event it started working. can anybody tell me please how to fix that. here is my code

    image.layout(0, 30, screenWidth, screenHeight - 30);
    image.setScaleType(ImageView.ScaleType.FIT_XY);


    params = new RelativeLayout.LayoutParams(screenWidth, screenHeight - 30);
    params.leftMargin = 0;
    params.topMargin = 30;


    layout.addView(image, params);

    image.setOnLongClickListener(new View.OnLongClickListener() {
        public boolean onLongClick(View v) {


            Log.i("its working", "its working");

            return true;
                    }
    });

    image.setOnTouchListener(this);

i,ll be very thankful.

thanks a lot.


回答1:


I think that the Object can have either an onClick or an onTouch, and it will use whatever the last one defined is. I have noticed this recently in some of my apps too.




回答2:


According to the developer docs

returning true from these event listeners will stop the propagation of the event to other event listeners and will also block the callback to the default event handler in the View.So be certain that you want to terminate the event when you return true

So perhaps returning false in your methods that handle the events would bring you a step closer to what you want to achieve




回答3:


I tried the "returning false". It will trig both of them. For the following example, it will show the different message on the Title.

public boolean onTouch(View v, MotionEvent event) {   
        // TODO Auto-generated method stub 
          int ea=event.getAction();                                    

          switch(ea){                                                       
          case MotionEvent.ACTION_MOVE:                                 
           int l=v.getLeft();                                       
           int b=v.getBottom();                                     
           int r=v.getRight();                                      
           int t=v.getTop();   
           String message = "l:"+l + "t:" + t+
             "r:"+r + "b:"+ b;
           this.setTitle(message);                                     
          }  
          return false;
}

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
        this.setTitle("Click");
}

@Override
public boolean onLongClick(View arg0) {
    // TODO Auto-generated method stub
    this.setTitle("Long Click");
    return false;
}



回答4:


it's working fine with me after Searching and Try and Error and Hope to work with you Well

1-add android:clickable="true" to ImageView in XML

2-in your Activity or View and make sure to return false ;

imageView.setOnTouchListener(new OnTouchListener() {
        @TargetApi(11)
        public boolean onTouch(View v, MotionEvent event) {
            // TODO Auto-generated method stub          
                Log.v(TAG, "Touched Here");
                return false;
        }
    });

3- then and last step make sure to return true;

imageView.setOnLongClickListener(new OnLongClickListener() {

        public boolean onLongClick(View v) {
            // TODO Auto-generated method stub
            Log.v(TAG, "Long Pressed Here");
            return true;
            }        
        });

it's will work well to Touch and Long Press



来源:https://stackoverflow.com/questions/3889735/android-setonlongclicklistner-does-not-work-with-ontouch-event

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