Android 滑动拦截实例代码解析
废话不多说了,直接给大家贴代码了,具体代码如下所示:
packagedemo.hq.com.fby; importandroid.content.Context; importandroid.util.AttributeSet; importandroid.util.Log; importandroid.view.MotionEvent; importandroid.widget.LinearLayout; /** *Createdbyhuqingon2016/12/7. *思路: *分内外部拦截 *在父布局中,onInterceptTouchEvent先判断是否拦截滑动,true拦截直接进入父布局的onTouch方法;false进入子布局的onTouch方法 */ publicclassMyParentViewextendsLinearLayout { /** *每向下Move移动的距离 */ privateintmMove; /** *落下点的位置 */ privateintyDown; /** *移动点的位置 */ privateintyMove; /** *一共向下挪动的距离 */ privateintdownDistance=0; publicMyParentView(Contextcontext,AttributeSetattrs) { super(context,attrs); } booleanintercept=false; /** *外部拦截, *如果是向下滑动则为true,交给该控件处理,向上false就交给子控件处理吧 *所以向上的事件子控件都能获取到的 * *@paramev *@return */ @Override publicbooleanonInterceptTouchEvent(MotionEventev) { switch(ev.getAction()) { caseMotionEvent.ACTION_DOWN: yDown=(int)ev.getY(); break; caseMotionEvent.ACTION_MOVE: yMove=(int)ev.getY(); if(yMove>yDown) { intercept=true; Log.d("hqq","拦截~~~~~~~~~~~~~~~~~~"); } elseif(yMove<yDown) { intercept=false; Log.d("hqq","不拦截~~~~~~~~~~~~~~~~~~"); } break; caseMotionEvent.ACTION_UP: break; } //true拦截,进入该控件的onTouchEvent方法false:进入子控件的OnTouchEvent booleanreturnInterCept=intercept; intercept=false; returnreturnInterCept; } @Override publicbooleanonTouchEvent(MotionEventevent) { Log.e("hq","fatheronTouch"); inty=(int)event.getY(); switch(event.getAction()) { caseMotionEvent.ACTION_DOWN: yDown=y; break; caseMotionEvent.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; caseMotionEvent.ACTION_UP: layout(getLeft(),getTop()-downDistance,getRight(),getBottom()-downDistance); downDistance=0; break; } returntrue;//returnsuper.onTouchEvent(event); } }
packagedemo.hq.com.fby; importandroid.content.Context; importandroid.util.AttributeSet; importandroid.util.Log; importandroid.view.MotionEvent; importandroid.widget.ScrollView; /** *Createdbyhuqingon2016/12/7. */ publicclassMyScrollViewextendsScrollView { publicMyScrollView(Contextcontext,AttributeSetattrs) { super(context,attrs); } @Override publicbooleanonTouchEvent(MotionEventev) { Log.e("hq","childonTouch----------------"); switch(ev.getAction()){ caseMotionEvent.ACTION_DOWN: getParent().requestDisallowInterceptTouchEvent(true); break; caseMotionEvent.ACTION_MOVE: if(getScrollY()==0){//ScrollView没有滑动时,即滑动高度没变化的话就允许父控件拦截 getParent().requestDisallowInterceptTouchEvent(false); }else{//禁止拦截 getParent().requestDisallowInterceptTouchEvent(true); } break; } returnsuper.onTouchEvent(ev); } }
<?xmlversion="1.0"encoding="utf-8"?> <RelativeLayout android:id="@+id/activity_main" xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" tools:context="demo.hq.com.fby.MainActivity"> <demo.hq.com.fby.MyParentView 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!"/> <demo.hq.com.fby.MyScrollViewandroid:layout_width="match_parent" android:layout_height="wrap_content"> <LinearLayoutandroid:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:layout_width="wrap_content" android:layout_height="200dp" android:text="HelloWorld!"/> <TextView android:layout_width="wrap_content" android:layout_height="200dp" android:text="HelloWorld!"/> </LinearLayout> </demo.hq.com.fby.MyScrollView> </demo.hq.com.fby.MyParentView> </RelativeLayout>
以上所述是小编给大家介绍的Android滑动拦截实例代码解析,希望对大家有所帮助,如果大家有任何疑问请给我留言,小编会及时回复大家的。在此也非常感谢大家对毛票票网站的支持!