滴水穿石
温故常能知新,多回头看看之前的代码,多想想以前的逻辑,你会发现豁然开朗。
简单的滑动冲突处理
今天在回顾之前的代码的时候,突然看到有个ScrollView在LinearLayout中冲突的处理方式,我简单的整理一下,做了一个简化版的小demo。
先看下我的main代码,
因为只是展示下view冲突,所以你没有看错,啥都没有。
自己写一个LinearLayout,重写onTouchEvent方法,吶:
public class MyMoveLayout extends LinearLayout { private int mMove; //动态获得滑动距离 private int mYDown,mYMove; //手指按下的位置和移动时手指的位置 private int mMoveBack; //回弹时需要回弹的距离 public MyMoveLayout(Context context, AttributeSet attrs) { super(context, attrs); } @Override public boolean onTouchEvent(MotionEvent event) { int y =(int) event.getY(); switch (event.getAction()){ case MotionEvent.ACTION_DOWN: mYDown=y; break; case MotionEvent.ACTION_MOVE: mYMove=y; if(mYMove-mYDown>0){ mMove=mYMove-mYDown; mMoveBack+=mMove; layout(getLeft(),getTop()+mMove,getRight(),getBottom()+mMove); } break; case MotionEvent.ACTION_UP: layout(getLeft(),getTop()-mMoveBack,getRight(),getBottom()-mMoveBack); mMoveBack=0; break; } return super.onTouchEvent(event); }
每次按下的时候获取手指按下的y位置,在滑动的时候将滑动的距离赋值给需要回弹的距离,然后通过layout进行移动和回弹。
但是大多数情况屏幕上需要向下移动的,这个时候在这个LinearLayout中加入ScrollView,加入之后发现,唉,回弹没用了,这个时候就开始冲突了,这个时候我在这个LinearLayout中加入了滑动拦截事件,也就是这个:
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { int y=(int) ev.getY(); switch (ev.getAction()){ case MotionEvent.ACTION_DOWN: mYDown=y; break; case MotionEvent.ACTION_MOVE: mYMove=y; if(mYMove-mYDown<0){ return false; }else if(mYMove-mYDown>0){ return true; } break; case MotionEvent.ACTION_UP: break; } return super.onInterceptTouchEvent(ev); }但是加入这个之后,一旦往下移除屏幕松开手指后,发现再也回不去了,那么现在我就在想,肯定是这个LinearLayout下面的ScrollView和LinearLayout冲突了,既然我们能够自己重写LinearLayout事件,那么我们是不是也能够重写ScrollView呢,接下来就是我重写的ScrollView的方法,吶:
public class MyScrollView extends ScrollView{ public MyScrollView(Context context) { super(context); } public MyScrollView(Context context, AttributeSet attrs) { super(context, attrs); } public MyScrollView(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); } @Override public boolean onTouchEvent(MotionEvent ev) { switch (ev.getAction()){ case MotionEvent.ACTION_MOVE: int y=getScrollY(); if(y==0){ getParent().requestDisallowInterceptTouchEvent(false);//让父view处理 }else{ getParent().requestDisallowInterceptTouchEvent(true);//自己处理 } break; } return super.onTouchEvent(ev); }哦,对了,还有Ui:
<com.example.wangguanghong.moveback.MyMoveLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent"> <com.example.wangguanghong.moveback.MyScrollView android:layout_width="match_parent" android:layout_height="match_parent"> <LinearLayout android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent"> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> <ImageView android:layout_width="match_parent" android:layout_height="match_parent" android:src="@mipmap/ic_launcher" /> </LinearLayout> </com.example.wangguanghong.moveback.MyScrollView> </com.example.wangguanghong.moveback.MyMoveLayout>很简单的一个页面,只是为了验证冲突。
但是加上那个之后,虽然看起来是可以了,但是多试几次发现,偶尔没有到达最上面也会有回弹的时候,这个肯定是不能容忍的,仔细想了想,在两个view的滑动的时候打印距离日志,发现没有什么问题,但是在LinearLayout中不管正常不正常回弹,那个ScrollY总是显示是0,那么我在想,是不是在ScrollView中,我们让父类处理那在处理的时候是不是有点冲突,然后我在判断之前加上了先让ScrollView处理,然后再根据距离来判断子View处理还是父View处理,最后代码是这样的:
public boolean onTouchEvent(MotionEvent ev) { getParent().requestDisallowInterceptTouchEvent(true); switch (ev.getAction()){ case MotionEvent.ACTION_MOVE: int y=getScrollY(); if(y==0){ getParent().requestDisallowInterceptTouchEvent(false); }else{ getParent().requestDisallowInterceptTouchEvent(true); } break; } return super.onTouchEvent(ev); }然后这个问题就解决了,但是现在还有一个体验小问题,这个就看大家测试功底了,解决其实也挺简单,试一下吧。
源码:https://github.com/MayIkiss/MoveBack
3454

被折叠的 条评论
为什么被折叠?



