ViewPager nested viewPager in Android cannot slide sub-viewPager, androidviewpager
The biggest headache is that the demand is not clear. No, there are three tabs in the parent viewPager, and a child viewPager is embedded in the first tab, at this time, the problem arises. The child viewPager cannot slide. I guess the parent viewPager intercepts the child viewPager, so I tried the first method below.
1. Override the onInterceptTouchEvent method of the parent viewPager.
/*** @ Author sub-ink ** 8:59:50 */public class MyViewPager extends ViewPager {public MyViewPager (Context context, AttributeSet attrs) {super (context, attrs );} @ Overridepublic boolean onInterceptTouchEvent (MotionEvent arg0) {return false ;}}
Of course there are many methods, you can also try to override the child viewPager
2. Override the child viewPager and notify the parent control not to intercept through the getParent (). requestDisallowInterceptTouchEvent (true) method.
Public class MyViewPager extends ViewPager {public MyViewPager (Context context) {super (context);} public MyViewPager (Context context, AttributeSet attrs) {super (context, attrs );} pointF downPoint = new PointF (); OnSingleTouchListener onSingleTouchListener; @ Override public boolean onTouchEvent (MotionEvent evt) {switch (evt. getAction () {case MotionEvent. ACTION_DOWN: // record the downPoint coordinate when it is pressed. x = Evt. getX (); downPoint. y = evt. getY (); if (this. getChildCount ()> 1) {// when there are more than one content, // notify its parent control. The current operation is performed on this control and the getParent () cannot be intercepted (). requestDisallowInterceptTouchEvent (true);} break; case MotionEvent. ACTION_MOVE: if (this. getChildCount ()> 1) {// when there are more than one content, // notify its parent control. The current operation is performed on this control and the getParent () cannot be intercepted (). requestDisallowInterceptTouchEvent (true);} break; case MotionEvent. ACTION_UP: // when the group is up, determine whether to press or the coordinate of the release operator to be a point if (Po IntF. length (evt. getX ()-downPoint. x, evt. getY ()-downPoint. y) <(float) 5.0) {onSingleTouch (this); return true;} break;} return super. onTouchEvent (evt);} public void onSingleTouch (View v) {if (onSingleTouchListener! = Null) {onSingleTouchListener. onSingleTouch (v) ;}} public interface OnSingleTouchListener {public void onSingleTouch (View v);} public void setOnSingleTouchListener (OnSingleTouchListener onSingleTouchListener) {this. onSingleTouchListener = onSingleTouchListener ;}}
Rewrite the child viewpager code from Hangzhou mountain not high
In fact, the above two methods are more inclined to the first, of course, the second can also solve the problem, but the second will destroy the event distribution process and directly tell the parent control not to intercept, I handle it myself, but it's always a bit overbearing. Haha, it's not in line with the moderate saying.
In this way, the problem can be solved, but the experience is poor, and I finally gave up. I just shared my problems and solutions. If you have a better way to communicate with each other.