Android-ViewPager: do not slide left (right) or left or right of viewpager

Source: Internet
Author: User

Android-ViewPager: do not slide left (right) or left or right of viewpager

Because of the project requirements, it is required to select a sliding month, and sliding to the current month is prohibited to slide left to the next month, Baidu is disabled, most of the two-way sliding between the left and right is not allowed, can not meet the effect, I went on to Baidu to learn about the distribution of touch events. I decided to write a try by myself, and the results were very good. A bug occurs at the beginning, so that the system can slide back and forth to the left. The result shows that the last coordinate is used only when the press is pressed to compare it with the current coordinate, which is definitely not satisfactory. This problem has been solved. Check the Code directly.

Package com. example. xxx; import android. content. context; import android. support. v4.view. viewPager; import android. util. attributeSet; import android. view. motionEvent;/*** ViewPager (one-way prohibited: Left sliding) * 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; // ---------- do not slide left and right ---------------- // @ Override // public boolean onTouchEvent (MotionEvent ev) {// if (isCanScroll) {// 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; //} // ----------------------------------------------- // ----- do not slide left ------- do Slide left: last coordinate> current coordinate @ Override public boolean dispatchTouchEvent (MotionEvent ev) {if (isCanScroll) {return super. dispatchTouchEvent (ev);} else {switch (ev. getAction () {case MotionEvent. ACTION_DOWN: // press it. If 'only' is used as the 'last coordinate ', it is inappropriate because it may be left slide and motionValue is greater than 0 (slide back and forth, as long as the stop coordinate is on the right of the pressed coordinate, the left slide can still slide.) beforeX = ev. getX (); break; case MotionEvent. ACTION_MOVE: float motionValue = ev. getX ()-beforeX; if (motionValue <0) {// do not slide left return true;} beforeX = ev. getX (); // when the finger moves, use the current coordinate as the next 'previous coordinate 'to solve the preceding problem: break; default: break;} return super. dispatchTouchEvent (ev) ;}} public boolean isScrollble () {return isCanScroll;}/*** set whether to slide * @ param isCanScroll */public void setScrollble (boolean isCanScroll) {this. isCanScroll = isCanScroll ;}}



Right-slide to change the motionValue judgment to a value greater than 0.

<pre name="code" class="java">if (motionValue > 0)

 

Related Article

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.