ViewPager viewPager = findViewById(R.id.viewPager); viewPager.setOnTouchListener(new View.OnTouchListener() { @Override public boolean onTouch(View v, MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: // Do something when the user touches the ViewPager break; case MotionEvent.ACTION_MOVE: // Do something when the user moves their finger on the ViewPager break; case MotionEvent.ACTION_UP: // Do something when the user lifts their finger from the ViewPager break; } return false; } });
public class MyViewPager extends ViewPager implements View.OnTouchListener { public MyViewPager(Context context) { super(context); setOnTouchListener(this); } @Override public boolean onTouch(View v, MotionEvent event) { // Do something when the user touches the custom ViewPager return false; } }This example creates a custom ViewPager that extends the default ViewPager class and implements the OnTouchListener interface. In the constructor, the custom ViewPager sets itself as a touch listener. When the user touches the custom ViewPager, some action would be performed. Overall, the setOnTouchListener() method in the android.support.v4.view package allows developers to customize the touch behavior of a ViewPager in a variety of ways.