Reprint Please specify source:http://blog.csdn.net/allen315410/article/details/40744287
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 see blog Android Custom control-Slide menu the bottom.
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 class Noscrollviewpager extends Viewpager {private Boolean noscroll = False;public Noscrollviewpager (context context, AttributeSet attrs) {Super (context, attrs);//TODO auto-generated Constructor stub}public Noscrollviewpager (context Context) {super (context);} public void Setnoscroll (Boolean noscroll) {this.noscroll = Noscroll;} @Overridepublic void ScrollTo (int x, int y) {super.scrollto (x, y);} @Overridepublic boolean ontouchevent (motionevent arg0) {/* return false;//super.ontouchevent (arg0); */if (Noscroll) Return False;elsereturn super.ontouchevent (arg0);} @Overridepublic boolean onintercepttouchevent (Motionevent arg0) {if (Noscroll) return False;elsereturn Super.onintercepttouchevent (arg0);} @Overridepublic void Setcurrentitem (int item, Boolean smoothscroll) {Super.setcurrentitem (item, smoothscroll);} @Overridepublic void Setcurrentitem (int item) {Super.setcurrentitem (item);}}
The above code is very simple, you can directly copy the use, no need to make any changes. 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.
<com.example.studydemo.view.noscrollviewpager android:id= "@+id/pager" android:layout_width= "Fill_ Parent " android:layout_height=" 0DP " android:layout_margintop=" 5DP " android:layout_weight=" 1 "/>
Well, this is very simple, no need to introduce too much. The code can be copied directly to use, layout or data adapter Pageradapter are identical to when using Viewpager. Well, if you have any better suggestions, you can leave a message below, I am happy to share with you to learn common progress!
Android prohibit Viewpager swipe left and right