3

On my layout, I have a ScrollView which hosts a VideoView and other TextViews.

My VideoView has a onTouch listener to start playing the video:

viPlayer.setOnTouchListener(new OnTouchListener() {
      @Override
      public boolean onTouch(View v, MotionEvent event) {
         if (isPlaying) {
            isPlaying = false;
            viPlayer.stopPlayback();
         } else {
            isPlaying = true;
            viPlayer.start();
         }
         return false;
      }
});

The problem I am facing is this: when the user puts the finger above the VideoView and starts to scroll down the ScrollView, the video starts to play. I want that when a Scroll is performed, the child views of ScrollView should not do anything. I see there is no listener for onScroll for ScrollView. How can I fix this?

akhilesh0707
  • 6,709
  • 5
  • 44
  • 51
Alin
  • 14,809
  • 40
  • 129
  • 218

1 Answers1

2

UPDATE Let me explain you what was happening. The child's touch events are always called first and only after the parent's event is called. What you are trying to do it's hard without some hacks. When you first touch on the video it's touch event is called and after that the scrollview's event is called. How can the video view know if you are going to scroll or not? It doesn't know upfront what you're trying to do. So how can you know if you should play or not the video?

So I tested this and this is working for me. Change your video touchevent to this. Remove the scrollview touch event. Notice the return true instead of return false.

        @Override
        public boolean onTouch(View view, MotionEvent motionEvent)
        {
            switch (motionEvent.getAction()){
                case MotionEvent.ACTION_UP:
                    if (isPlaying) {
                     isPlaying = false;
                     viPlayer.stopPlayback();
                    } else {
                     isPlaying = true;
                     viPlayer.start();
                    }
                    break;
            }
            return true;
        }
Pedro Oliveira
  • 20,442
  • 8
  • 55
  • 82
  • this sound like a really great solution, however when I try to set the touch listener to `ScrollView` I get this: `The method requestDisallowInterceptTouchEvent(boolean) is undefined for the type VideoView` so I am guessing that this is not supported for VideoView ? – Alin Oct 06 '14 at 17:42
  • Well that means that it doesn't have that method. Try this. Set a boolean field. And set it to false instead of that disallow call and too true on the other one. Then check that flag on the video's touch event. I'm not on my computer but when I get home I'll update the answer – Pedro Oliveira Oct 06 '14 at 17:46
  • Ok I've edited the solution. Notice that I've changed the code for the video player touch listener. You simple just need to control whether are you clicking or not on the scrollview – Pedro Oliveira Oct 06 '14 at 18:02
  • it is not working. `viPlayer` `onTouchListener` only receives event ACTION_DOWN and no ACTION_UP which is really weird... – Alin Oct 06 '14 at 18:14
  • That was probably some mistake I did. I've edited the viPlayer touchListener. Change the if to inside the ACTION_UP case. – Pedro Oliveira Oct 06 '14 at 18:16
  • thank you for your time. As I said, ACTION_UP does not trigger at all. So the video never plays. – Alin Oct 06 '14 at 18:31
  • Well that's weird. Then remove the switch and try like this. See answer. – Pedro Oliveira Oct 06 '14 at 18:32
  • Removing the switch makes the onTouch work together with scroll :) Basically same thing as initially. – Alin Oct 06 '14 at 18:35
  • Change the initial value of touchFlag to true. – Pedro Oliveira Oct 06 '14 at 18:36
  • Hmmm, now it works initially while I scroll down. when I scroll up it plays the video too. Probably I'll have to reinit touchFlag somewhere – Alin Oct 06 '14 at 18:44
  • Set touchFlag to true just before the `return false;` of your video's touch listener – Pedro Oliveira Oct 06 '14 at 19:36
  • This should be the final solution. Good luck – Pedro Oliveira Oct 07 '14 at 10:02
  • You are so right! While you answered I just noticed as you said that the ScrollView onTouch happens after PlayerView onTouch. I've implemented your solution and it works like a charm. Many thanks. – Alin Oct 07 '14 at 10:12