最近的项目中需要添加右滑退出这个功能,在这里我是直接自定义一个view来实现。话不多说直接上代码。
package com.cad.view;
import android.content.Context;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import java.util.ArrayList;
import java.util.List;
public class SlidingFinishFrameForLToRLayout extends FrameLayout {
private ViewGroup mParentView;
private int mWidth;
private int mHeight;
private int mDownX, mDownY, mMoveX, mMoveY;
private ViewPager mTouchView;
private List mList_PassView= new ArrayList<>();
private boolean DEBUG= false;
private OnSlidingFinish onSlidingFinish;
private int mTouchSlopValue; //滑动到超过此距离则表示有效操作
public SlidingFinishFrameForLToRLayout(Context context) {
this(context, null);
}
public SlidingFinishFrameForLToRLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SlidingFinishFrameForLToRLayout(Context context, AttributeSet attrs,
int defStyle) {
super(context, attrs, defStyle);
mTouchSlopValue= ViewConfiguration.get(context).getScaledTouchSlop();
}
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
super.onLayout(changed, l, t, r, b);
if (changed) {
mParentView= (ViewGroup) this.getParent();
this.mWidth= this.getWidth();
this.mHeight= this.getHeight();
}
}
public void setOnSlidingFinish(OnSlidingFinish onSlidingFinish) {
this.onSlidingFinish= onSlidingFinish;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
switch (ev.getAction()) {
case MotionEvent.ACTION_DOWN:
mDownX= (int) ev.getRawX();
mDownY= (int) ev.getRawY();
if (DEBUG)
System.out.println("tag-n debug 6-23 onInterceptTouchEvent checkIsTouchPassView() " + checkIsTouchPassView());
if (checkIsTouchPassView()) return false;
break;
case MotionEvent.ACTION_MOVE:
mMoveX= (int) ev.getRawX();
mMoveY= (int) ev.getRawY();
// System.out.println("tag-n onInterceptTouchEvent MotionEvent.ACTION_MOVE-------"+((mMoveY - mDownY) > 50 && Math.abs(mMoveX - mDownX) < 50));
if ((mMoveX- mDownX) > 50 && Math.abs(mMoveY- mDownY) < 50) { //left to right
// if (-(mMoveX - mDownX) > 50 && Math.abs(mMoveY - mDownY) < 50) { //right to left
// if ((mMoveY - mDownY) > 50 && Math.abs(mMoveX - mDownX) < 50) { // top to bottom
// viewpager item 2 is lyric fragment
if (checkIsTouch(mTouchView, mDownX, mDownY) && (mTouchView!= null && mTouchView.getCurrentItem() != 0)) {
return super.onInterceptTouchEvent(ev);
} else {
return true;
}
}
break;
case MotionEvent.ACTION_UP:
// if(-(mMoveX-mDownX) > 180){
// onSlidingFinish.onSlidingFinish();
// return true;
// }
break;
default:
break;
}
return super.onInterceptTouchEvent(ev);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// mDownX = (int) event.getRawX();
// mDownY = (int)event.getRawY();
// break;
boolean isTouchPassView = checkIsTouchPassView();
if (DEBUG)
System.out.println("tag-n debug 6-23 onTouchEvent isTouchPassView " + isTouchPassView);
if (isTouchPassView) return false;
return true;
case MotionEvent.ACTION_MOVE:
mMoveX= (int) event.getRawX();
mMoveY= (int) event.getRawY();
break;
case MotionEvent.ACTION_UP:
// System.out.println("tag-n debug onTouchEvent MotionEvent.ACTION_UP-------"+((mMoveY - mDownY) > 130 && Math.abs(mMoveX - mDownX) < 130));
if ((mMoveX- mDownX) > 130 && Math.abs(mMoveY- mDownY) < 130) { //left to right
// if(-(mMoveX-mDownX) > 130 && Math.abs(mMoveY - mDownY) <130){ //right to left
// if ((mMoveY - mDownY) > 130 && Math.abs(mMoveX - mDownX) < 130) { // top to bottom
isTouchPassView = checkIsTouchPassView();
if (DEBUG)
System.out.println("tag-n debug 6-23 onTouchEvent isTouchPassView " + isTouchPassView);
if (isTouchPassView) return false;
if(onSlidingFinish!= null) onSlidingFinish.onSlidingFinish(checkIsTouch(mTouchView, mDownX, mDownY));
break;
}
break;
default:
break;
}
return super.onTouchEvent(event);
}
/*public boolean checkIsTouch(View touchView, int downX, int downY) {
if (touchView == null) return false;
Rect vRect = new Rect();
touchView.getHitRect(vRect);
boolean isTouch = vRect.contains(downX, downY);
if (DEBUG) {
System.out.println("tag-n debug Sliding downX --- " + downX);
System.out.println("tag-n debug Sliding downY --- " + downY);
System.out.println("tag-n debug Sliding vRect.top --- " + vRect.top);
System.out.println("tag-n debug Sliding vRect.bottom --- " + vRect.bottom);
System.out.println("tag-n debug Sliding vRect.left --- " + vRect.left);
System.out.println("tag-n debug Sliding vRect.right --- " + vRect.right);
System.out.println("tag-n debug Sliding isTouch -- " + isTouch);
}
return isTouch;
}*/
private boolean checkIsTouch(View touchView, int downX, int downY) {
if (touchView == null) {
return false;
}
int[] location = new int[2];
touchView.getLocationOnScreen(location);
int left = location[0];
int top = location[1];
int right = left + touchView.getMeasuredWidth();
int bottom = top + touchView.getMeasuredHeight();
boolean isTouch = downY >= top && downY <= bottom && downX >= left
&& downX <= right;
if (DEBUG) {
System.out.println("tag-n debug Sliding downX --- " + downX);
System.out.println("tag-n debug Sliding downY --- " + downY);
System.out.println("tag-n debug Sliding vRect.top --- " + top);
System.out.println("tag-n debug Sliding vRect.bottom --- " + bottom);
System.out.println("tag-n debug Sliding vRect.left --- " + left);
System.out.println("tag-n debug Sliding vRect.right --- " + right);
System.out.println("tag-n debug Sliding isTouch --- " + isTouch);
}
return isTouch;
}
private boolean checkIsTouchPassView() {
for (int i = 0; i < mList_PassView.size(); i++) {
if (checkIsTouch(mList_PassView.get(i), mDownX, mDownY)) {
return true;
}
}
return false;
}
public void setViewPager(ViewPager viewpager) {
if (viewpager != null) {
mTouchView= viewpager;
// Rect VRect = new Rect();
// viewpager.getHitRect(VRect);
}
}
public void setPassView(View passView) {
if (passView != null) {
mList_PassView.add(passView);
}
}
public void removePassView(View passView) {
if (passView != null) {
mList_PassView.remove(passView);
}
}
public interface OnSlidingFinish {
void onSlidingFinish(boolean isInViewPager);
}
}
在XML中 使用 如图所示:
然后就是在activity 里面调用:
mSlidingFinishFrameForLToRLayout.setOnSlidingFinish(new SlidingFinishFrameForLToRLayout.OnSlidingFinish() {
@Override
public void onSlidingFinish(boolean isInViewPager) {
finish();
}
});
如果有跟viewpage 有冲突,则在setOnSlidingFinish 之前调用下面的方法
mSlidingFinishFrameForLToRLayout.setViewPager(getViewpage());
网友评论