Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way to do this is to create a custom ViewPager and override the <code>onTouchEvent()</code> and <code>onTouchInterceptEvent()</code> methods. For example,</p> <pre><code>public class LockedViewPager extends ViewPager { private boolean pagingEnabled; public LockedViewPager(Context context, AttributeSet attrs) { super(context, attrs); pagingEnabled = true; } @Override public boolean onTouchEvent(MotionEvent event) { if (pagingEnabled) { return super.onTouchEvent(event); } return false; } @Override public boolean onInterceptTouchEvent(MotionEvent event) { if (pagingEnabled) { return super.onTouchEvent(event); } return false; } /** * @param pagingEnabled * if true, ViewPager acts as normal * if false, ViewPager cannot be scrolled by a user's touch */ public void setPagingEnabled(boolean pagingEnabled) { this.pagingEnabled = pagingEnabled; } } </code></pre> <p>Whenever you'd like to prevent the user from being able to scroll, simply call <code>lockedViewPager.setPagingEnabled(false);</code><br> And then when you'd like to turn it back on, <code>lockedViewPager.setPagingEnabled(true);</code></p> <p>Just to give a brief explanation, the onTouchEvent methods are normally the entry point for handling paging behavior, so we're simply stopping it right at the beginning whenever paging is not enabled. Also, the methods normally return true if they have handled the event, so we must return false to indicate that we have ignored it. </p> <p>In order to incorporate this into your app, first add the LockedViewPager to your activity's XML, but don't forget to the name of <code>your_app</code> and <code>your_package</code> to create the path to your LockedViewPager class (if you're using Android Studio, beginning to type LockedViewPager should automatically recognize and suggest the path for you):</p> <pre><code>&lt;com.your_app.your_package.LockedViewPager android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/lockedViewPager"/&gt; </code></pre> <p>Then, you would use this in your activity like so:</p> <pre><code>public class MyActivity { private LockedViewPager lockedViewPager; public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // other activity setup, etc. lockedViewPager = (LockedViewPager) findViewById(R.id.lockedViewPager); // set up pager, set its adapter and any listeners, etc. } /* * Call this method once you've zoomed, for example */ public void onZoom() { lockedViewPager.setPagingEnabled(false); } /* * Don't forget to call this method at some time later to re-enable the paging */ public void resumePaging() { lockedViewPager.setPagingEnabled(true); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload