Andorid: Custom ViewPager -- the child ViewPager slides directly to the edge and then directly slides the parent ViewPager

Source: Internet
Author: User

Andorid: Custom ViewPager -- the child ViewPager slides directly to the edge and then directly slides the parent ViewPager

In a recent project, we have a requirement to use nested ViewPager in ViewPager to achieve the whole effect. After no processing is done, we can only keep sliding the child ViewPager, and the parent ViewPager cannot slide, this is definitely not satisfactory. After analyzing the cause, it is shown that the whole sliding event is consumed by ViewPager. Therefore, you can only customize ViewPager. The following are the requirements after implementation:

  

We can see from the figure that ViewPager is nested in ViewPager. What we need to achieve is to slide the parent ViewPager when the child ViewPager slides to the last page. next we will post the custom ViewPager code for your reference. <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD4KPHA + oaGhoTwvcD4KPHByZSBjbGFzcz0 = "brush: java; "> /************************************* * ********************** @ File Name: myViewPager. java * @ Creation Time: 4:24:25, January 1, November 18, 2014 * @ modification history: create the initial version ************************************** on April 9, November 18, 2014 ************************************ * *******************/public class MyViewPager extends ViewPager {// reference of the parent ViewPager private ViewPager viewPager; private boolean flag = true; private float mLastMotionX; public BookstoreViewPager (Context context) {super (context) ;}public BookstoreViewPager (Context context, AttributeSet attrs) {super (context, attrs);} public ViewPager getViewPager () {return viewPager;} // this method must be called before processing to initialize ViewPagerpublic void setViewPager (ViewPager viewPager) {this. viewPager = viewPager;} @ Overridepublic boolean dispatchTouchEvent (MotionEvent ev) {final float x = ev. getX (); switch (ev. getAction () {case MotionEvent. ACTION_DOWN: // The parent control does not process any touch event viewPager. requestDisallowInterceptTouchEvent (true); flag = true; mLastMotionX = x; break; case MotionEvent. ACTION_MOVE: if (flag) {if (x-mLastMotionX> 5 & getCurrentItem () = 0) {flag = false; viewPager. requestDisallowInterceptTouchEvent (false); // submit the event to the parent Control for processing} if (x-mLastMotionX <-5 & getCurrentItem () = getAdapter (). getCount ()-1) {flag = false; viewPager. requestDisallowInterceptTouchEvent (false) ;}} break; case MotionEvent. ACTION_UP: viewPager. requestDisallowInterceptTouchEvent (false); break; case MotionEvent. ACTION_CANCEL: viewPager. requestDisallowInterceptTouchEvent (false); break;} return super. dispatchTouchEvent (ev );}}

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.