title: Detecting touch/tap events within a viewpager
body: <p>I'm quite new to android development and at the moment I'm working with a viewpager, trying to understand how I can handle touch/tap events. I've spent all morning searching for answers to this and I can't seem to find anything that helps me.</p>
  
  <p>Basically I have a viewpager with 5 fragments that all use the same layout. All I want to do is be able to detect when there's a tap/touch on the stationary view (I don't want any detection to happen when a view is being dragged).</p>
  
  <p>From what I gather, there seems to be an issue with the ViewPager intercepting all touch events by default. I believe this can be dealt with by using:</p>
  
  <pre><code>myPager.requestDisallowInterceptTouchEvent(true);
  </code></pre>
  
  <p>At first I dabbled with OnTouchListener and tried to log whether the onTouch method was ever hit - it appeared not. I've also seen the GestureDetector class thrown about a lot as a solution to this problem but frankly I feel overwhelmed and I'm not sure which classes/interfaces I should be using here.</p>
  
  <p>All I'd like to do is pick up simple touch/tap events while using a ViewPager. </p>
  
  <p>At the moment I'm using a modified version of the Animation tutorial code from the android developer site. This is what I have in my <strong>MainActivity.java</strong> file:</p>
  
  <pre><code>package com.example.pbinteractive;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.support.v4.app.FragmentActivity;
  import android.support.v4.app.FragmentManager;
  import android.support.v4.app.FragmentStatePagerAdapter;
  import android.support.v4.view.PagerAdapter;
  import android.support.v4.view.ViewPager;
  
  public class MainActivity extends FragmentActivity {
      /**
       * The number of pages (wizard steps) to show in this demo.
       */
      private static final int NUM_PAGES = 5;
  
      /**
       * The pager widget, which handles animation and allows swiping horizontally to access previous
       * and next wizard steps.
       */
      private ViewPager mPager;
  
      /**
       * The pager adapter, which provides the pages to the view pager widget.
       */
      private PagerAdapter mPagerAdapter;
  
      @Override
      protected void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          setContentView(R.layout.activity_main);
  
          // Instantiate a ViewPager and a PagerAdapter.
          mPager = (ViewPager) findViewById(R.id.pager);
          mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
          mPager.setAdapter(mPagerAdapter);       
      }   
  
      /**
       * A simple pager adapter that represents 5 {@link ScreenSlidePageFragment} objects, in
       * sequence.
       */
      private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
          public ScreenSlidePagerAdapter(FragmentManager fm) {
              super(fm);
          }        
  
          @Override
          public Fragment getItem(int position) {
              return MyPageFragment.create(position);
          }
  
          @Override
          public int getCount() {
              return NUM_PAGES;
          }
      }   
  }
  </code></pre>
  
  <p>And this is what I have in my <strong>MyPageFragment.java</strong> file:</p>
  
  <pre><code>package com.example.pbinteractive;
  
  import android.os.Bundle;
  import android.support.v4.app.Fragment;
  import android.view.LayoutInflater;
  import android.view.MotionEvent;
  import android.view.View;
  import android.view.View.OnTouchListener;
  import android.view.ViewGroup;
  
  public class MyPageFragment extends Fragment implements OnTouchListener {
      /**
       * The argument key for the page number this fragment represents.
       */
      public static final String ARG_PAGE = "page";
  
      /**
       * The fragment's page number, which is set to the argument value for {@link #ARG_PAGE}.
       */
      private int mPageNumber;
  
      /**
       * Factory method for this fragment class. Constructs a new fragment for the given page number.
       */
      public static MyPageFragment create(int pageNumber) {
          MyPageFragment fragment = new MyPageFragment();
          Bundle args = new Bundle();
          args.putInt(ARG_PAGE, pageNumber);
          fragment.setArguments(args);
          return fragment;
      }
  
      public MyPageFragment() {
      }
  
      @Override
      public void onCreate(Bundle savedInstanceState) {
          super.onCreate(savedInstanceState);
          mPageNumber = getArguments().getInt(ARG_PAGE);
      }
  
      @Override
      public View onCreateView(LayoutInflater inflater, ViewGroup container,
              Bundle savedInstanceState) {
          // Inflate the layout containing a title and body text.
          ViewGroup rootView = (ViewGroup) inflater
                  .inflate(R.layout.fragment_slide, container, false);
  
          // Set the title view to show the page number.
          //((TextView) rootView.findViewById(android.R.id.text1)).setText(
          //        getString(R.string.title_template_step, mPageNumber + 1));
  
          return rootView;
      }
  
      /**
       * Returns the page number represented by this fragment object.
       */
      public int getPageNumber() {
          return mPageNumber;
      }
  
      @Override
      public boolean onTouch(View v, MotionEvent event) {
          // TODO Auto-generated method stub
          return false;
      }   
  }
  </code></pre>
  
  <p>FYI I've checked both of these resources, as they often seem to be cited as the best solution to the problem I'm facing (from what I can gather):</p>
  
  <p><a href="https://stackoverflow.com/questions/6920137/android-viewpager-and-horizontalscrollview">android: ViewPager and HorizontalScrollVIew</a></p>
  
  <p><a href="https://stackoverflow.com/questions/937313/android-basic-gesture-detection">Fling gesture detection on grid layout</a></p>
  
  <p>But I haven't been able to get much help from them. Like I said I'm quite new to working with android so I'd really like to just get familiar with the basics!</p>
  
  <p>Thanks</p>
  
  <p>EDIT: Just to clarify, I'd like to pick up the touch events within each <strong>fragment</strong>, since once I get past this hurdle I'd like some fragments to have touch events working while others will just display unresponsive content.</p>