Sometimes there are some "weird" requirements in development, such as embedding a ListView in Viewpager, or embedding a viewpager, which causes the embedded Xxview to slide when sliding. So now the outermost viewpager is forbidden to slide, let the embedded Xxview get the sliding event. About the solution, there are many statements on the Internet, basically the same, but need to understand the Android event distribution mechanism, do not understand the event distribution mechanism, the Internet to look up some information to see, and then I have a brief introduction here, please refer to the blog Android custom control-slide menu below.
How to forbid viewpager to slide around? Is roughly rewriting Viewpager, covering Viewpager's onintercepttouchevent (Motionevent arg0) method and Ontouchevent (Motionevent arg0) method, The return values of both methods are of type Boolean and only need to change the return value to False, then Viewpager will not consume the finger swipe event, and pass it to the upper view to handle or the event will terminate directly. Here is my custom viewpager.
Public classNoscrollviewpagerextendsViewpager {Private BooleanNoscroll =true; PublicNoscrollviewpager (Context context, AttributeSet attrs) {Super(context, attrs); //TODO auto-generated Constructor stub } PublicNoscrollviewpager (Context context) {Super(context); } Public voidSetnoscroll (Booleannoscroll) { This. Noscroll =Noscroll; } @Override Public voidScrollTo (intXinty) {Super. ScrollTo (x, y); } @Override Public Booleanontouchevent (motionevent arg0) {/*return false;//super.ontouchevent (arg0);*/ if(Noscroll)return false; Else return Super. Ontouchevent (arg0); } @Override Public Booleanonintercepttouchevent (motionevent arg0) {if(Noscroll)return false; Else return Super. Onintercepttouchevent (arg0); } @Override Public voidSetcurrentitem (intItemBooleansmoothscroll) { Super. Setcurrentitem (item, Smoothscroll); } @Override Public voidSetcurrentitem (intItem) { Super. Setcurrentitem (item); }}
It can be copied directly, without any modification. For ease of operation, I have set a Boolean control variable in this custom Viewpager, and have provided a way to control whether the Viewpager is not allowed to slide, which is a bit more flexible. The following is a definition of a custom Viewpager in a layout file.
114, Android prohibit Viewpager left and right sliding