How to disable/ lock one page in viewpager?
Asked Answered
S

5

6

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.

enter image description here

I disabled tab click for Events page by using following code :

    LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));
if(!isAccess){
            tabStrip.getChildAt(3).setClickable(false);
            tabStrip.getChildAt(3).setEnabled(false);
}

But now I need to restrict swiping for that page in view pager. How to do this?

Sticker answered 17/6, 2016 at 4:47 Comment(3)
locking one tab seems to be pretty bad UX. Why not just have a simple page that says events are unavailable (or some relevant message) when the user swipes to the events tab?Corker
Better idea would be only show available options according to the user. I.e. if user has access to Event tab, render 4 tabs else render only 3 tabs.Jung
this code not working, it returs null: LinearLayout tabStrip = ((LinearLayout)tabLayout.getChildAt(0));Pinky
S
3

I written this in pager adapter it works.

@Override
public int getCount() {
  if(!isAccess) {
    return 3;
  } else {
    return 4;
  }
}
Sticker answered 17/6, 2016 at 5:17 Comment(3)
This is not according to OP's requirement. If he implements this code, only 3 tabs will get rendered in TabStrip also and Event tab won't get even visible on screen. OP wants to just disable/lock last tab.Jung
It seems that your question doesn't match with your answer. You want to disable/lock tab but in your answer the 4th tab will not visible. Its static kind of static solution.Tobe
Yes this what I want. the 4th tab will be rendered / visible according to scenario.Sticker
S
3

Use this

viewPager.setOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}

public void onPageSelected(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) { }
});
Steato answered 17/6, 2016 at 5:1 Comment(2)
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() is depecated.Sticker
setSelected has the signature public void setSelected(boolean selected), so calling it with 2 should be the same as calling it with false, right?Donahoe
S
3

I written this in pager adapter it works.

@Override
public int getCount() {
  if(!isAccess) {
    return 3;
  } else {
    return 4;
  }
}
Sticker answered 17/6, 2016 at 5:17 Comment(3)
This is not according to OP's requirement. If he implements this code, only 3 tabs will get rendered in TabStrip also and Event tab won't get even visible on screen. OP wants to just disable/lock last tab.Jung
It seems that your question doesn't match with your answer. You want to disable/lock tab but in your answer the 4th tab will not visible. Its static kind of static solution.Tobe
Yes this what I want. the 4th tab will be rendered / visible according to scenario.Sticker
A
1

This is also the solution without using the deprecated method (setOnPageChangeListener).

        viewPager.addOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
        @Override
        public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
            super.onPageScrolled(position, positionOffset, positionOffsetPixels);
        }

        @Override
        public void onPageSelected(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
            }
        }

        @Override
        public void onPageScrollStateChanged(int state) {
            super.onPageScrollStateChanged(state);
        }
    });    
Aidaaidan answered 3/10, 2019 at 6:45 Comment(0)
H
0

Initialize the first tab position

int currentTab = 0;

then make listener for viewPager

viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                //4 is the position of the tab we want to disable
                if (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;
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
Honea answered 15/4, 2020 at 6:50 Comment(0)
S
0

To stop specific tab from being selected just use customized TabLayout and ViewPager you can even use the tab for other purposes this way.

    public class CustomViewPager extends ViewPager {
    private float initialXValue;
    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event)
    {
        if(event.getAction()==MotionEvent.ACTION_DOWN)
            initialXValue = event.getX();
        return this.isSwipeAllowed(event) && super.onInterceptTouchEvent(event);
    }
    private boolean isSwipeAllowed(MotionEvent event) {
        //you can prevent any swipe from right to left for an item at a specified index, in your case getCurrentItem() returns 3 for Event tab 
        return getCurrentItem()!=3 || event.getAction() != MotionEvent.ACTION_MOVE || (event.getX() - initialXValue) > 0;
    }
    }

Custom TabLayout

    public class CustomTabLayout extends TabLayout {

    public CustomTabLayout(Context context,AttributeSet attrs) {
        super(context, attrs);
    }
    public CustomTabLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    @Override
    public void selectTab(Tab tab, boolean updateIndicator) {
        // it doesn't allow the item on index 3 to be selected in anyway
        if (!(tab.getPosition() == 3))
            super.selectTab(tab, updateIndicator);
    }
}
Skiplane answered 24/4, 2023 at 21:30 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.