How To Disable/ Lock One Page In Viewpager?
I want lock / disable one particular page(fragment) in viewpager. According to scenario for some people only able to access that page. How restrict swiping to that page. I disabl
Solution 1:
I written this in pager adapter it works.
@Override
publicintgetCount() {
if(!isAccess) {
return3;
} else {
return4;
}
}
Solution 2:
Use this
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
publicvoidonPageScrollStateChanged(int state) {}
publicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
publicvoidonPageSelected(int position) {
if (position == 3)
{
// show dialog that you are not allowed
viewPager.setSelected(2);
}
}
});
Hope this helped you
You can also use
viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
// optional @Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) { }
// optional @Override
public void onPageSelected(int position) { }
// optional @Override
public void onPageScrollStateChanged(int state) { }
});
Solution 3:
This is also the solution without using the deprecated method (setOnPageChangeListener).
viewPager.addOnPageChangeListener(newViewPager.SimpleOnPageChangeListener() {
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
super.onPageScrolled(position, positionOffset, positionOffsetPixels);
}
@OverridepublicvoidonPageSelected(int position) {
super.onPageSelected(position);
if(position == 3) {
//To disable specific tab and set the previuos tab
viewPager.setCurrentItem(2); //We cannot provide the position in setSelected(boolean) now. This was missed in above solution
}
}
@OverridepublicvoidonPageScrollStateChanged(int state) {
super.onPageScrollStateChanged(state);
}
});
Solution 4:
Initialize the first tab position
int currentTab = 0;
then make listener for viewPager
viewPager.addOnPageChangeListener(newViewPager.OnPageChangeListener() {
@OverridepublicvoidonPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
@OverridepublicvoidonPageSelected(int position) {
//4 is the position of the tab we want to disableif (position == 4) {
if (currentTab < 4) {
// swipe from left to right
viewPager.setCurrentItem(5);
currentTab = 5;
} else {
// swipe from right to left
viewPager.setCurrentItem(3);
currentTab = 3;
}
} else {
currentTab = position;
}
}
@OverridepublicvoidonPageScrollStateChanged(int state) {
}
});
Post a Comment for "How To Disable/ Lock One Page In Viewpager?"