114, Android prohibit Viewpager left and right sliding

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.