androidandroid-fragmentspagerslidingtabstrip

make page scroll disable in pagerslidingtabstrip


Can we disable page sliding in PagerSlidingTabStrip ? I am using PagerSlidingTabStrip where tabs are created dynamically in my example. On each fragment there are tow buttons

  1. Chapter wise analysis
  2. Rank wise analysis

What i want is when user click on Rank wise analysis Sliding should disable and tabs get hide and when click on Chapter wise analysis tabs should visible again and sliding should also enable. How to achieve this? Tabs hide and show is working properly but am not able to make slide disable.


Solution

  • Create a separate class which should extend ViewPager:

    public class CustomViewPager extends ViewPager {
    
    public CustomViewPager(Context context) {
        super(context);
    }
    
    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
    
    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }
    
    @Override
    public boolean onTouchEvent(MotionEvent event) {
        // Never allow swiping to switch between pages
        return false;
    }
    }
    

    And then use this viewPager to set in your xml file like this:

    <com.packagename.CustomViewPager
        android:id="@+id/view_pager"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    

    Then in your activity, set your FragmentPagerAdapter to this viewPager:

    mViewPager.setAdapter(YourFragmentPagerAdapter)