智能手机的大屏化,使得单手操作和侧滑返回这两个功能成为了移动开发中常见的需求,本文说的就是侧滑开源组件SwipeBack在常见布局中的一些冲突问题。
需求场景:Editext设置单行显示许多文字时,需要左右滑动输入框内的文字,例如顶部搜索输入框。此时右滑会与侧滑返回冲突
解决方案:
使用自定义view判断Editext何时滑动到最左端再去控制整个布局是否侧滑
public class MyEditext extends android.support.v7.widget.AppCompatEditText {
private ScrollViewListener scrollViewListener = null;
private boolean can_touch;
public interface ScrollViewListener {
void onScrollChanged(MyEditext editext, int x, int y,
int oldx, int oldy);
}
public MyEditext(Context context) {
super(context);
}
public MyEditext(Context context, AttributeSet attrs) {
super(context, attrs);
}
public MyEditext(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(can_touch);
}
return super.dispatchTouchEvent(ev);
}
public void setCan_touch(boolean can_touch) {
this.can_touch = can_touch;
}
@Override
protected void onScrollChanged(int l, int t, int oldl, int oldt) {
super.onScrollChanged(l, t, oldl, oldt);
if (scrollViewListener != null) {
scrollViewListener.onScrollChanged(this, l, t, oldl, oldt);
}
}
public void setScrollViewListener(ScrollViewListener scrollViewListener) {
this.scrollViewListener = scrollViewListener;
}
}
使用此自定义view时(signatureEdit为MyEditext自定义view)
核心代码:
private boolean leftEnd;//判断是否滑动到最左端
signatureEdit.setScrollViewListener(new MyEditext.ScrollViewListener() {
@Override
public void onScrollChanged(MyEditext v, int x, int y, int oldx, int oldy) {
if (v.getScrollX() > 0) {
leftEnd = false;
} else {
leftEnd = true;
}
}
});
signatureEdit.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent event) {
EditTextOnTouch(view,event,signatureEdit,leftEnd);
return false;
}
});
private void EditTextOnTouch(View view, MotionEvent event,MyEditext myEditext,boolean flag) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (view instanceof EditText) {
if (flag){
myEditext.setCan_touch(false);
}else {
myEditext.setCan_touch(true);
}
setEnableSwipe(flag);
}
break;
case MotionEvent.ACTION_UP:
setEnableSwipe(true);
break;
case MotionEvent.ACTION_CANCEL:
setEnableSwipe(true);
break;
}
}
注释 setEnableSwipe((boolean enableSwipe)为设置是否支持滑动返回,上面分享的开源代码的基类中已包含
需求场景:RecyclerView滑动方向和SwipeBack滑动方向一致,滑动出现冲突
解决方案:
同样判断RecyclerView何时滑动到最左端再去控制整个布局是否侧滑
可以外层包一个自定义ScrollView,也可以直接判断RecyclerView是否滑动到最左端,根据需求而定,我这是需要外层加个ScrollView
核心代码:
自定义横向ScrollView
public class MySwipeRecyclerView extends RecyclerView {
boolean isIntercept=true;
public MySwipeRecyclerView(Context context) {
super(context);
}
public MySwipeRecyclerView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
}
public MySwipeRecyclerView(Context context, @Nullable AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
if (getParent() != null) {
getParent().requestDisallowInterceptTouchEvent(isIntercept);
}
return super.dispatchTouchEvent(ev);
}
public void setIntercept(boolean intercept) {
isIntercept = intercept;
}
使用 (photoscrv为自定义ScrollView)
private boolean isleftEnd;
photoscrv.setScrollViewListener(new MyHorizontalScrollView.ScrollViewListener() {
@Override
public void onScrollChanged(MyHorizontalScrollView v, int x, int y, int oldx, int oldy) {
if (v.getScrollX() > 0) {
isleftEnd = false;
} else {
isleftEnd = true;
}
}
});
photoRecyclerview.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
break;
case MotionEvent.ACTION_MOVE:
if (v instanceof RecyclerView) {
setEnableSwipe(isleftEnd);
}
break;
case MotionEvent.ACTION_UP:
setEnableSwipe(true);
break;
case MotionEvent.ACTION_CANCEL:
setEnableSwipe(true);
break;
}
return true;
}
});
SwipeBack github地址:https://github.com/zhibuyu/SwipeBackDemo