Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

关于引用该项目遇到的一些问题,以及解决办法 #41

Open
cuiMarker opened this issue Aug 10, 2017 · 0 comments
Open

关于引用该项目遇到的一些问题,以及解决办法 #41

cuiMarker opened this issue Aug 10, 2017 · 0 comments

Comments

@cuiMarker
Copy link

cuiMarker commented Aug 10, 2017

1.设置左滑之后,下滑禁止不掉
这个问题是因为你的Activity即继承了SwipeBackActivity,Activity的布局中又加上了SwipeBackLayout。解决办法是要么继承SwipeBackActivity,然后在onCreate直接setDragEdge()设置;要么再布局文件中加上SwipeBackLayout,然后在Activty里调用SwipeBackLayout的setDragEdge()方法;两者只能选一个。
2.Activity中有webview或者ListView、Recyclerview等可以上下拖动的控件时,侧滑很容易被触发。
这个问题是滑动冲突照成的,位于底部的SwipeBackLayout拦截了子View的触摸事件。所以我们要做的是继承SwipeBackLayout重写它的onInterceptTouchEvent()方法,当手指在Y轴的移动速度大于X轴的移动速度时不拦截这个触摸事件,代码如下:
public class MySwipeBackLayout extends SwipeBackLayout{
private float startY;
private float startX;
// 记录viewPager是否拖拽的标记
private boolean mIsVpDragger;
private int mTouchSlop;
public MySwipeBackLayout(Context context) {
super(context);
}
public MySwipeBackLayout(Context context, AttributeSet attrs) {
super(context, attrs);
mTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
setDragEdge(DragEdge.LEFT);
}
@OverRide
public boolean onInterceptTouchEvent(MotionEvent ev) {
int action = ev.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
// 记录手指按下的位置
startY = ev.getY();
startX = ev.getX();
// 初始化标记
mIsVpDragger = false;
break;
case MotionEvent.ACTION_MOVE:
// 如果viewpager正在拖拽中,那么不拦截它的事件,直接return false;
if(mIsVpDragger) {
return false;
}
// 获取当前手指位置
float endY = ev.getY();
float endX = ev.getX();
float distanceX = Math.abs(endX - startX);
float distanceY = Math.abs(endY - startY);
// 如果X轴位移大于Y轴位移,那么将事件交给viewPager处理。
/if(distanceX > mTouchSlop && distanceX > distanceY) {
mIsVpDragger = true;
return false;
}
/
if(distanceY > mTouchSlop && distanceY > distanceX) {
mIsVpDragger = true;
return false;
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
// 初始化标记
mIsVpDragger = false;
break;
}
// 如果是Y轴位移大于X轴,事件交给SwipeBackLayout处理。
return super.onInterceptTouchEvent(ev);
}
}
3.布局中有个viewpager或者横向的recyclerview时,向右滑触发SwipeBackLayout侧滑。
这也是滑动冲突照成的,我们要做的是重写viewpager或recyclerview的dispatchTouchEvent()方法。
代码如下:
public class MyRecyclerView extends RecyclerView{
public MyRecyclerView(Context context) {
super(context);
}
public MyRecyclerView(Context context, @nullable AttributeSet attrs) {
super(context, attrs);
}
public MyRecyclerView(Context context, @nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@OverRide
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getParent() != null){
getParent().requestDisallowInterceptTouchEvent(true);
}
return super.dispatchTouchEvent(ev);
}
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant