Preface
Nowadays, many apps do not allow sliding switching on the home page (because the page is loading), but they use viewpage to manage frgament because it is convenient.
Examples found online before:
public class NoScrollViewPager extends ViewPager { public NoScrollViewPager(Context context, AttributeSet attrs) { super(context, attrs); } public NoScrollViewPager(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { //No intercept, otherwise the child will not be able to receive the event. Generally, this customization will not be handled. return (ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { return (ev); } @Override public boolean onTouchEvent(MotionEvent ev) { return true; }
If the inherited Viewpager is of lower version, then there is no problem.
But if you use a higher version of API when compiling, it will be invalid after 5.0.
You will find that there will be slight sliding. It can still slide.
Just tell whether the old one is new or new one, just look at the setOnPageChangeListener
This method in the latest Viewpager has been abandoned and changed toaddOnPageChangeListener()
It's right.
Because I changed the viewpager to lazy loading before, I have always kept an old version of Viewpager to use. I didn't find it until recently.
After improvement
public class NoScrollViewPager extends ViewPager { private boolean isScroll; public NoScrollViewPager(Context context,AttributeSetattrs{ super(context, attrs); } public NoScrollViewPager(Context context) { super(context); } /** * No treatment is done in general *, If the default return value is modified, the child will not receive the event */ @Override public boolean dispatchTouchEvent(MotionEvent ev) { return (ev); // return true; no } /** * Whether to intercept * Intercept: will go into your onTouchEvent method * No intercept: The incident is passed to the child */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { // return false;// feasible, do not intercept events, // return true;//No, the child cannot handle the event //return (ev);//No, there will be subtle movement if (isScroll){ return (ev); }else{ return false; } } /** * Whether to consume events * Consumption: The event ends * Not consumed: pass to the parent control */ @Override public boolean onTouchEvent(MotionEvent ev) { //return false;// feasible, not consumed, passed to the parent control //return true;// feasible, consumption, intercept events //(ev); //No, //Although onInterceptTouchEvent is intercepted, //But if the child control in the viewpage is not a viewgroup, this method will still be called. if (isScroll){ return (ev); }else { return true;// Feasible, consumption, intercept incidents } } public void setScroll(boolean scroll) { isScroll = scroll; } }
Summarize
The above is the entire content of this article. I hope the content of this article will be of some help to your study or work. If you have any questions, you can leave a message to communicate. Thank you for your support.