2

I could disable ViewPager with extending ViewPager to have a setPagingEnabled method, but now I just want to disable a child temporarily.

I suspect I have to override OnPageScrolled and cancel scrolling by accessing the setScrollState method of ViewPager, but I can't figure out how.

Herrbert74
  • 2,188
  • 26
  • 47
  • I'm not sure what you mean by child? What is the type of your child view? "disable" can mean a lot of things...? – Sam Dozor Jan 13 '12 at 14:58
  • 1
    Sorry if I wasn't clear. By child I meant a page, a fragment what is paged. Disable is to disable paging to that fragment. – Herrbert74 Jan 13 '12 at 15:05
  • I can't believe nobody can answer this. I was able to figure out that I can't access setScrollState, because it's private in ViewPager. Also if I want to temporarily disable scrolling to left, I can intercept a left swipe in OnPageScrolled and I can override onInterceptTouchEvent in ViewPager. But how to connect them both? – Herrbert74 Jan 14 '12 at 21:16
  • I have the same problem. I have ViewPager and ImageViewbs witch pinch zoom as childrens. when I zoom the image and want to swipe it, the touch event goes also to ViewPager and the page is switched. I want to filter touch events in childerns so that they are not always passed to parent ViewPager. Were you able to implement that? – cubesoft Jan 20 '12 at 23:16

2 Answers2

7

thats my solution for it:

public class NonSwipeableViewPager extends ViewPager {
    private boolean swipe = true;

    public boolean isSwipe() {
        return swipe;
    }

    public void setSwipe(boolean swipe) {
        this.swipe = swipe;
    }

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

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

    @Override
    public boolean onInterceptTouchEvent(MotionEvent arg0) {

        if (swipe) {
            return super.onInterceptTouchEvent(arg0);
        }

        // Never allow swiping to switch between pages
        return false;
    }

}
Arthur Neves
  • 11,197
  • 8
  • 55
  • 72
  • 1
    No, sorry, this is the setPagingEnabled method I mentioned, but with an other name. I needed a method to disable paging to a page. For example disable the paging to a page#2, but not to page#0 and #1. – Herrbert74 May 03 '12 at 10:55
  • This is exactly what I have been trying to make. Do you know a way to define a custom ui element like this in an xml though? Similar to how I could define a normal ViewPager in an xml? – bhekman Jul 03 '12 at 01:56
  • Nevermind, I can google, lol! Found it: http://stackoverflow.com/questions/2695646/declaring-a-custom-android-ui-element-using-xml Thanks for the post! – bhekman Jul 03 '12 at 02:09
1

This is what I've done to disable the first page of a ViewPager. Instead of using state variables, I used ViewPager.getAdapter() and checked there if I should allow the swipe:

public class CardViewPager extends ViewPager {

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

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

    @Override
    public void setCurrentItem(int item) {
        if ((((CardFragmentAdapter) this.getAdapter()).isFirstItemDisabled())
                && (item == 0)) {
            Log.d("ViewPager", "You don't wanna see this.");
            return;
        }
        super.setCurrentItem(item);
    }
}

Of course this is only logical if you don't want to remove the page for some reason.

If for example you want to disable the second item, you can super.setCurrentItem(item+1) or something similar.