Android-viewpager No left slide (right slide)

Source: Internet
Author: User

because the project needs, to do a sliding month selection of the effect, and sliding to the current month will be forbidden to slide left to the next one months, Baidu, mostly is forbidden to the left and right two-way sliding, to meet the effect, continue to Baidu understand the next touch event distribution, I decided to write a try, the results are very good . began to appear a bug, sliding back and forth can still slide the past, the results found that only when pressed as the last coordinate, and the current coordinates, there will definitely be an unsatisfied situation. This problem has been solved, look directly at the code bar.

Package Com.example.xxx;import Android.content.context;import Android.support.v4.view.viewpager;import Android.util.attributeset;import android.view.motionevent;/** * Can be set to prevent sliding of the viewpager (One-way prohibition: swipe left) * Core method: Setscrollble () *     @author Alan * */public class Canotslidingviewpager extends Viewpager {/** * last x-coordinate */private float Beforex;        Public Canotslidingviewpager (context context, AttributeSet Attrs) {Super (context, attrs);    TODO auto-generated Constructor stub} public Canotslidingviewpager (context context) {super (context);     } Private Boolean iscanscroll = true; ----------disable left and right swipe------------------//@Override//public boolean ontouchevent (motionevent ev) {//if (Iscans Croll) {//return super.ontouchevent (EV),//} else {//return false;//}////}///         /@Override//public boolean onintercepttouchevent (Motionevent arg0) {////TODO auto-generated method stub// if (Iscanscroll){//return super.onintercepttouchevent (ARG0);/} else {//return false;//}////} -------------------------------------------//-----Disable left slide-------left: Previous coordinates > current coordinates @Override publi    C Boolean dispatchtouchevent (Motionevent ev) {if (Iscanscroll) {return super.dispatchtouchevent (EV); }else {switch (ev.getaction ()) {case motionevent.action_down://pressed if ' only ' as ' Last coordinates ', improper, because there may be left slip, Motionvalue greater than 0 case (sliding back and forth, as long as the stop coordinates at the right of the press coordinates, left slide can still slip past) Beforex = Ev.getx (); Break;case motionevent.action _move:float Motionvalue = Ev.getx ()-Beforex;if (Motionvalue < 0) {//disable left slip return true;} Beforex = Ev.getx ();//When the finger moves, the current coordinates are used as the next ' last coordinates ' to solve the above problem break;default:break;}        return super.dispatchtouchevent (EV);}    } public boolean isscrollble () {return iscanscroll; }/** * Set whether you can swipe * @param iscanscroll */public void setscrollble (Boolean iscanscroll) {this.is    CanScroll = Iscanscroll; }}



Right slide to change the Motionvalue judgment to be greater than 0

<pre name= "code" class= "Java" >if (Motionvalue > 0)


Android-viewpager No left slide (right slide)

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.