Android-the realization of ViewPager prohibiting left and right sliding

table of Contents

1 background

2 Custom ViewPager implementation

3 How to use


1 background

Use ViewPager+BottomNavigationView+multiple Fragments to quickly build a page switching architecture. One has four pages. Because of testing needs, you need to block the middle two. The method is:

  • Set non-clickable options: In the xml layout file, the app:menu attribute of BottomNavigationView is the item of each page, and the android:enabled attribute of the page item that you do not want to be displayed is set to false, and the corresponding page cannot be displayed at this time;

  • Set BottomNavigationView not to slide: But BottomNavigationView has no corresponding method to support this function.

2 Custom ViewPager implementation

[Principle]: Write a CustomViewPager class to inherit ViewPager, then rewrite onTouchEvent, onInterceptTouchEvent, and add the setSlidingEnable method to enable or disable sliding by external parameters. Here is a brief explanation of the onInterceptTouchEvent method:

  • Role: Determine whether to intercept the touch event above;
  • Parameters:
    ——return false: Do not intercept the event, and release the event. The event will be passed to the child control of the current View, which will be distributed and processed by the dispatchTouchEvent method in the child control;
    ——return true: intercept the event and hand it over to the onTouchEvent method of the current View for processing;

[Code]:

public class CustomViewPager extends ViewPager {

    // the sliding page switch
    private boolean isSlidingEnable = true ;

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

    public CustomViewPager(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    //重写此函数
    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        return  this.isSlidingEnable;
    }
    //重写此函数
    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        return this.isSlidingEnable;
    }

    public void setSlidingEnable(boolean slidingEnable) {
        isSlidingEnable = slidingEnable;
    }
}

3 How to use

private CustomViewPager homeViewPager = null;
homeViewPager = (CustomViewPager) findViewById(R.id.view_pager_home);
//中间涉及的其他步骤就不赘述了
​​​​​​​homeViewPager.setSlidingEnable(false);  //禁止左右滑动

There is a problem to note here: CustomViewPager is a custom class, we need to use this CustomViewPager when we define the layout file xml, otherwise we still use the original androidx.viewpager.widget.ViewPager, here homeViewPager = (CustomViewPager) findViewById( R.id.view_pager_home); will report an error because the type does not match:

———————————————————————————————————

This article is the original article of the blogger, please indicate the source for reprinting!

If this article is helpful to you, lift up your wealthy little hand, follow/comment/like/collect , this is my greatest support!

I wish you a promotion and a salary increase, a great future!

Guess you like

Origin blog.csdn.net/w464960660/article/details/109387520