- 菜单处于侧滑时,拦截长按事件
- 解决侧滑时 点击 的冲突
- 通过 isSwipeEnable 变量控制是否开启右滑菜单,默认打开
…
使用一个叫SwipeMenuLayout的自定义View,先上代码(注释详细):
public class SwipeMenuLayout extends ViewGroup {
private static final String TAG = "zxt";
private int mScaleTouchSlop;
private int mMaxVelocity;
private int mPointerId;
private int mHeight;
private int mRightMenuWidths;
private int mLimit;
private View mContentView;
private PointF mLastP = new PointF();
private boolean isUnMoved = true;
private PointF mFirstP = new PointF();
private boolean isUserSwiped;
private static SwipeMenuLayout mViewCache;
private static boolean isTouching;
private VelocityTracker mVelocityTracker;
private android.util.Log LogUtils;
private boolean isSwipeEnable;
private boolean isIos;
private boolean iosInterceptFlag;
private boolean isLeftSwipe;
public SwipeMenuLayout(Context context) {
this(context, null);
}
public SwipeMenuLayout(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public SwipeMenuLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init(context, attrs, defStyleAttr);
}
public boolean isSwipeEnable() {
return isSwipeEnable;
}
public void setSwipeEnable(boolean swipeEnable) {
isSwipeEnable = swipeEnable;
}
public boolean isIos() {
return isIos;
}
public SwipeMenuLayout setIos(boolean ios) {
isIos = ios;
return this;
}
public boolean isLeftSwipe() {
return isLeftSwipe;
}
public SwipeMenuLayout setLeftSwipe(boolean leftSwipe) {
isLeftSwipe = leftSwipe;
return this;
}
public static SwipeMenuLayout getViewCache() {
return mViewCache;
}
private void init(Context context, AttributeSet attrs, int defStyleAttr) {
mScaleTouchSlop = ViewConfiguration.get(context).getScaledTouchSlop();
mMaxVelocity = ViewConfiguration.get(context).getScaledMaximumFlingVelocity();
isSwipeEnable = true;
isIos = true;
isLeftSwipe = true;
TypedArray ta = context.getTheme().obtainStyledAttributes(attrs, R.styleable.SwipeMenuLayout, defStyleAttr, 0);
int count = ta.getIndexCount();
for (int i = 0; i < count; i++) {
int attr = ta.getIndex(i);
if (attr == R.styleable.SwipeMenuLayout_swipeEnable) {
isSwipeEnable = ta.getBoolean(attr, true);
} else if (attr == R.styleable.SwipeMenuLayout_ios) {
isIos = ta.getBoolean(attr, true);
} else if (attr == R.styleable.SwipeMenuLayout_leftSwipe) {
isLeftSwipe = ta.getBoolean(attr, true);
}
}
ta.recycle();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setClickable(true);
mRightMenuWidths = 0;
int contentWidth = 0;
int childCount = getChildCount();
final boolean measureMatchParentChildren = MeasureSpec.getMode(heightMeasureSpec) != MeasureSpec.EXACTLY;
boolean isNeedMeasureChildHeight = false;
for (int i = 0; i < childCount; i++) {
View childView = getChildAt(i);
childView