Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

requestDisallowInterceptTouchEvent does not work unless selecting view first

According to android docs you can get your parent ViewGroup and call requestDisallowInterceptTouchEvent(true) on it to stop other things from interfering. This causes not only the immediate parent but any other parent objects that might intercept the touch to ignore it for the duration of the particular event...

This sounds great and seems to work fine on newer devices (mine is android 4.1) but older devices (i.e. 2.3.3) it does not work unless I click on my scroll view first and then scroll it, otherwise other parent scrollable views may still interfere.

I'm sending the request in the View.OnTouchListener for the scrollable child.

Any idea how to make this work automatically without resorting to writing custom subclasses to check the hit rect on the motion event, etc?

like image 737
linuxfreakus Avatar asked Jan 29 '13 15:01

linuxfreakus


2 Answers

I had some problems too with 2.3 where it the disallow would intermittenltly work.

I used to call view.requestDisallowInterceptTouchEvent(true) regardless of the event.getAction().

Then I tried to be a good citizen and changed my code in the onTouch() method to the following:

switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            v.requestDisallowInterceptTouchEvent(true);
            break;
        case MotionEvent.ACTION_CANCEL:
        case MotionEvent.ACTION_UP:
            v.requestDisallowInterceptTouchEvent(false);
            break;
        default:
            break;
        }

Remember that this method (or some other views under the referenced view) has to return true for the parents to adhere to the disallow request.

Not sure this will fix your issue but worth a try.

like image 120
sciutand Avatar answered Nov 07 '22 08:11

sciutand


You can try this one:

m_parentScrollView.setOnTouchListener(new View.OnTouchListener() 
{
       public boolean onTouch(View p_v, MotionEvent p_event) 
        {
               m_childScrollView.getParent().requestDisallowInterceptTouchEvent(false);
           //  We will have to follow above for all scrollable contents
           return false;
        }
});

                                        **OR**

m_childScrollView.setOnTouchListener(new View.OnTouchListener() 
{
      public boolean onTouch(View p_v, MotionEvent p_event)
       {
          // this will disallow the touch request for parent scroll on touch of child view
           p_v.getParent().requestDisallowInterceptTouchEvent(true);
           return false;
       }
});
like image 21
Maddy Avatar answered Nov 07 '22 08:11

Maddy