ViewPager shields the scrolling animation when sliding left and right and switching

We often use a similar combination of ViewPager+Fragment+TabLayout.

Then there are various demands.

Sometimes it is required that the viewpager cannot slide left and right, but the scrolling animation when switching is retained.
Sometimes it is required that the viewpager cannot slide left and right, and the scrolling animation does not appear when switching.

[Solution]
In order to achieve the above requirements, we need to rewrite ViewPager.

For prohibiting ViewPager from sliding left and right:
Override ViewPager's onInterceptTouchEvent(MotionEvent arg0) method
and onTouchEvent(MotionEvent arg0) method.
The return values ​​of these two methods are both boolean types. You only need to change the return value to false
so that ViewPager will not be consumed The event of finger sliding is passed to the upper View for processing or the event is terminated directly.

For static viewpager switching animation:
you need to rewrite the setCurrentItem() method of viewpager

The custom NoScrollViewPager class is as follows, which can be directly copied and pasted for use.

/** 
 * Used to shield viewpager from sliding left and right and scrolling animation when switching 
 * Forbid ViewPager from sliding left and right: 
 * ViewPager needs to be rewritten to override ViewPager's onInterceptTouchEvent(MotionEvent arg0) method 
 * and onTouchEvent(MotionEvent arg0) method 
 * these two The return value of the method is boolean type, you only need to change the return value to false 
 * so that ViewPager will not consume the event of finger sliding, and then pass it to the upper View for processing or the event will be terminated directly. 
 * 
 * Static viewpager switching animation: 
 * You need to rewrite the viewpager's setCurrentItem() method 
 */ 
public class NoScrollViewPager extends ViewPager { 

    private boolean isCanScroll = true; 
    private boolean isHasScrollAnim=true; 

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

    public NoScrollViewPager(Context context, AttributeSet attrs) {
        super(context, attrs); 
    } 
    @Override
 
    /**
     * Set whether it can slide 
     * @param isCanScroll false prohibits sliding, true can slide 
     */ 
    public void setCanScroll(boolean isCanScroll) { 
        this.isCanScroll = isCanScroll; 
    } 

    /** 
     * Set whether to remove the sliding effect 
     * @param isHasScrollAnim false remove scrolling Effect, true does not remove 
     */ 
    public void setHasScrollAnim(boolean isHasScrollAnim){ 
        this.isHasScrollAnim=isHasScrollAnim; 
    } 


    @Override 
    public boolean onInterceptTouchEvent(MotionEvent ev) { 
        return isCanScroll && super.onInterceptTouchEvent(ev); 
    } 

    public boolean onTouchEvent(MotionEvent ev) {
        return isCanScroll && super.onTouchEvent(ev); 
    } 

    @Override 
    public void setCurrentItem(int item, boolean smoothScroll) { 
        super.setCurrentItem(item, smoothScroll); 
    } 

    /** 
     * Set whether to seek scrolling animation when switching 
     *isHasScrollAnim When false, the scrolling effect will be removed 
     */ 
    @Override 
    public void setCurrentItem(int item) { 
        super.setCurrentItem(item,isHasScrollAnim); 
    } 

}

Among them, setHasScrollAnim(boolean isHasScrollAnim) is used to set whether to remove the sliding effect,

setCanScroll(boolean isCanScroll) is used to set whether to slide

Guess you like

Origin blog.csdn.net/s_nshine/article/details/130845669