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.