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 );}}