1

I'm doing some prototyping for my second app which will be using a TabLayout and ViewPager to display a series of 4 pages in a specific order so that the user can do a task in a logical manner. I will need to make sure that the user doesn't jump from an early page to a later page until the work on the early page is completed. Is that even possible in a TabLayout/ViewPager app?

All the examples I have seen so far demonstrate three tabs and show the user swiping effortlessly from one to the next and back again. None of the examples even HINT that it's possible to prevent swiping (or tabbing) to one of the other pages until a given condition is met. I need to understand if it's even POSSIBLE to limit swiping/tabbing in an app like this.

I would like the user to finish the "work" on the first page and then signal that he/she is done by clicking a NEXT button. The user should NOT be able to proceed to another page unless NEXT has been pressed. (The NEXT button is essentially the user's way of telling the app that he's finished with the first page.) By the same token, if a user is on one of the later pages, I don't want him to be able to go back to a prior page in most pages: I want to write program logic that decides if it's okay to take him back to a previous page based on what has happened so far.

Can I do what I'm proposing in Android Studio 3.1.4? If yes, what are the main classes/methods I will use to control the swiping and tabbing?

If this is NOT possible in Android, approach SHOULD I use to build my app?

Henry
  • 1,395
  • 1
  • 12
  • 31

1 Answers1

1

Use Lockable View Pager to disable swipe in viewpager from user input touch

public class LockableViewPager extends ViewPager {

private boolean swipeLocked;

public LockableViewPager(Context context) {
    super(context);
}

public LockableViewPager(Context context, AttributeSet attrs) {
    super(context, attrs);
}

public boolean getSwipeLocked() {
    return swipeLocked;
}

public void setSwipeLocked(boolean swipeLocked) {
    this.swipeLocked = swipeLocked;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return !swipeLocked && super.onTouchEvent(event);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
    return !swipeLocked && super.onInterceptTouchEvent(event);
}

@Override
public boolean canScrollHorizontally(int direction) {
    return !swipeLocked && super.canScrollHorizontally(direction);
}

}

After setting up viewpager in Tablayout try below code for tablayout to disable tab switching on touch

LinearLayout tabStrip = ((LinearLayout)mTabLayout.getChildAt(0));
for(int i = 0; i < tabStrip.getChildCount(); i++) {
    tabStrip.getChildAt(i).setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            return true;
        }
    });
}

Refer for tab touch selection disabling.

https://stackoverflow.com/a/37810306/3787344

SimpleCoder
  • 1,665
  • 1
  • 21
  • 34
  • Thank you, that's very helpful. I'm just going to leave the answer as is pending other answers that may appeal to me even more. – Henry Aug 11 '18 at 10:33