Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android ViewPager when swiping on last screen

Im having trouble figuring out how to capture a swipe event on the last page of a view pager.

Basically the requirement is that when the user is on the last page, and they try to swipe to get to the next page, the activity should close.

I've tried doing this onPageScrolled but I cant seem to differentiate a left or right swipe when in there. When on the last page, the user should still maintain the functionality to move to the previous page.

So basically the case is

When on last page
    if swipe prev (do normal behaviour)
    if swipe next (finish activity)

Can anyone provide any suggestions? Thanks

like image 801
SikhWarrior Avatar asked Mar 26 '14 22:03

SikhWarrior


People also ask

How do I turn off ViewPager swipe?

You can easily do that by creating a custom class inherits from viewPager and override two methods: “onTouchEvent()” and “onInterceptTouchEvent()” and return true or false to disable and enable the swiping on certain touch events i.e say swiping.

How do I swipe ViewPager on Android?

Implement Swipe Views You can create swipe views using AndroidX's ViewPager widget. To use ViewPager and tabs, you need to add a dependency on ViewPager and on Material Components to your project. To insert child views that represent each page, you need to hook this layout to a PagerAdapter .

What is ViewPager Android?

ViewPager in Android allows the user to flip left and right through pages of data. In our android ViewPager application we'll implement a ViewPager that swipes through three views with different images and texts.

How do I turn off swipe in ViewPager Kotlin?

There is no built in way to disable swiping between pages of a ViewPager, what's required is an extension of ViewPager that overrides onTouchEvent and onInterceptTouchEvent to prevent the swiping action. To make it more generalised we can add a method setSwipePagingEnabled to enable/disable swiping between pages.


2 Answers

Here is working solution!!

Required variables

....
private boolean isLastPageSwiped;
private int counterPageScroll;
....

Inside onPageScrolled of ViewPager

@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
    //Here 6 is last position 
    if (position == 6 && positionOffset == 0 && !isLastPageSwiped){
        if(counterPageScroll != 0){
            isLastPageSwiped=true;
            //Next Activity here
        }
        counterPageScroll++;
    }else{
        counterPageScroll=0;
    }
}
like image 178
Kishore Jethava Avatar answered Sep 21 '22 12:09

Kishore Jethava


You should include an fake blank page to your ViewPager, when user swipes your actual last page, it comes to this fake one. When this fake one is visible finish the Activity.

You should try this way, if it does not work, I can give you an example code.

like image 33
tasomaniac Avatar answered Sep 18 '22 12:09

tasomaniac