I won’t say much nonsense, I will just post the code to you. The specific code is as follows:
package ; import ; import ; import ; import ; import ; /** * Created by huqing on 2016/12/7. * Idea: * Internal and external interception * In the parent layout, onInterceptTouchEvent first determines whether to intercept the slide, true intercepts the onTouch method that directly enters the parent layout; false enters the onTouch method that enters the child layout */ public class MyParentView extends LinearLayout { /** * Distance per downward movement */ private int mMove; /** * The position of the drop point */ private int yDown; /** * The position of the moving point */ private int yMove; /** * The total distance to move downward */ private int downDistance = 0; public MyParentView(Context context, AttributeSet attrs) { super(context, attrs); } boolean intercept = false; /** * External interception, * If it is sliding down, it is true, and it is handed over to the control for processing. If it is false upward, it is handed over to the subcontrol for processing. * So the upward events can be obtained by sub-controls * * @param ev * @return */ @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (()) { case MotionEvent.ACTION_DOWN: yDown =(int) (); break; case MotionEvent.ACTION_MOVE: yMove = (int) (); if (yMove > yDown) { intercept = true; ("hqq", "Intercept~~~~~~~~~~~~~~~~~~~~~~~~~~"); } else if (yMove < yDown) { intercept = false; ("hqq", "No intercepting~~~~~~~~~~~~~~~~~~~~~~~~~~~~"); } break; case MotionEvent.ACTION_UP: break; } //true intercept, enter the onTouchEvent method of the control false: enter the OnTouchEvent of the child control boolean returnInterCept = intercept; intercept = false; return returnInterCept; } @Override public boolean onTouchEvent(MotionEvent event) { ("hq", "father onTouch"); int y = (int) (); switch (()) { case MotionEvent.ACTION_DOWN: yDown = y; break; case MotionEvent.ACTION_MOVE: if (downDistance>=250){ }else { yMove = y; if (yMove - yDown > 0) { mMove = yMove - yDown; downDistance += mMove; if (downDistance>=250){ layout(getLeft(),downDistance, getRight(), getHeight() + downDistance); }else { layout(getLeft(), getTop() + mMove, getRight(), getBottom() + mMove); } } } break; case MotionEvent.ACTION_UP: layout(getLeft(), getTop() - downDistance, getRight(), getBottom() - downDistance); downDistance = 0; break; } return true;// return (event); } }
package ; import ; import ; import ; import ; import ; /** * Created by huqing on 2016/12/7. */ public class MyScrollView extends ScrollView { public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent ev) { ("hq","child onTouch----------------"); switch (()){ case MotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(true); break; case MotionEvent.ACTION_MOVE: if (getScrollY()==0){//When the ScrollView is not sliding, that is, if the sliding height does not change, the parent control is allowed to intercept it. getParent().requestDisallowInterceptTouchEvent(false); }else {//Offering is prohibited getParent().requestDisallowInterceptTouchEvent(true); } break; } return (ev); } }
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout android: xmlns:andro xmlns:tools="/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context=""> < android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#ffffff" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="100dp" android:text=" World!"/> <TextView android:layout_width="wrap_content" android:layout_height="100dp" android:text=" World!"/> < android:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="200dp" android:text="Hello World!"/> <TextView android:layout_width="wrap_content" android:layout_height="200dp" android:text="Hello World!"/> </LinearLayout> </> </> </RelativeLayout>
The above is the code analysis of Android sliding interception example introduced by the editor. I hope it will be helpful to everyone. If you have any questions, please leave me a message and the editor will reply to everyone in time. Thank you very much for your support for my website!