The suggested answers are more complicated than I thought. So I created a simple one.
I used OnPageChangeListener in the ViewPager class. Here we have 3 methods. void onPageScrolled (int position, float positionOffset, int positionOffsetPixels) void onPageSelected (int position); void onPageScrollStateChanged (int state);
It is important to know what are the states in onPageScrollStateChanged () and their sequence of execution. There are 3 states like SCROLL_STATE_IDLE, SCROLL_STATE_DRAGGING and SCROLL_STATE_SETTLING.
Here is the execution order. Scroll_state_dragging → Scroll_state_settling → onPageSelected () → Scroll_state_idle
The idea is for the flag inside onPageSelected () to write the current page number. Then, if the user is on the last page and left-click scroll_state_dragging and run the following view.
private int pagePosition; // keep a class variable private int[] layouts= new int[]{ R.layout.welcome_slide1, R.layout.welcome_slide2, R.layout.welcome_slide3}; ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() { @Override public void onPageSelected(int position) { addBottomDots(position); pagePosition = position; } @Override public void onPageScrolled(int position, float positionOffset, int arg2) { } @Override public void onPageScrollStateChanged(int state) { if (state == ViewPager.SCROLL_STATE_DRAGGING) { if (pagePosition == layouts.length - 1) { launchHomeScreen(); } } } }; private void launchHomeScreen() { startActivity(new Intent(IntroductionActivity.this, MainDockerActivity.class)); finish(); }
I created a complete example here.
source share