Skip to content Skip to sidebar Skip to footer

How Can I Use Onclicklistener In Parent View And Ontouchevent In Child View?

I have a Custom Layout with a standard setOnClickListener() call. setOnClickListener(new OnClickListener() { @Override public void onClick(View v) {

Solution 1:

The short answer is yes, you can achieve this behavior. The long answer is that you need to make sure that both views get the touch events as you expect them. Depending on the exact layouts and the expected behavior you want, you can use a couple of methods:

  1. You don't have to return true from onTouchEvent() - that way the touch event would go to the parent as well (just make sure that you are getting the next event).
  2. Use onInterceptTouchEvent() in the parent and detect the click by using a GestureDetector and SimpleOnGestureListener.

    GestureDetector.SimpleOnGestureListenersimpleOnGestureListener=newGestureDetector.SimpleOnGestureListener(){
        @OverridepublicbooleanonSingleTapUp(MotionEvent e) {
            // some click related codereturntrue;
        }
    };
    
    GestureDetectorgestureDetector=newGestureDetector(context, simpleOnGestureListener);
    
    @OverridepublicbooleanonInterceptTouchEvent(MotionEvent e){
        gestureDetector.onTouchEvent(e);
        returnfalse;
    }
    

Post a Comment for "How Can I Use Onclicklistener In Parent View And Ontouchevent In Child View?"