下面列出了怎么用android.support.v4.widget.ViewDragHelper的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (isDragLocked()) {
return super.onInterceptTouchEvent(ev);
}
mDragHelper.processTouchEvent(ev);
mGestureDetector.onTouchEvent(ev);
accumulateDragDist(ev);
boolean couldBecomeClick = couldBecomeClick(ev);
boolean settling = mDragHelper.getViewDragState() == ViewDragHelper.STATE_SETTLING;
boolean idleAfterScrolled = mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE
&& mIsScrolling;
// must be placed as the last statement
mPrevX = ev.getX();
// return true => intercept, cannot trigger onClick event
return !couldBecomeClick && (settling || idleAfterScrolled);
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null && context != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.SwipeRevealLayout,
0, 0
);
mDragEdge = a.getInteger(R.styleable.SwipeRevealLayout_dragFromEdge, DRAG_EDGE_LEFT);
mMode = MODE_NORMAL;
mMinFlingVelocity = DEFAULT_MIN_FLING_VELOCITY;
mMinDistRequestDisallowParent = DEFAULT_MIN_DIST_REQUEST_DISALLOW_PARENT;
}
mDragHelper = ViewDragHelper.create(this, 1.0f, mDragHelperCallback);
mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);
mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}
@Override
public void onEdgeDragStarted(int edgeFlags, int pointerId) {
super.onEdgeDragStarted(edgeFlags, pointerId);
if (mLockDrag) {
return;
}
boolean edgeStartLeft = (mDragEdge == DRAG_EDGE_RIGHT)
&& edgeFlags == ViewDragHelper.EDGE_LEFT;
boolean edgeStartRight = (mDragEdge == DRAG_EDGE_LEFT)
&& edgeFlags == ViewDragHelper.EDGE_RIGHT;
if (edgeStartLeft || edgeStartRight) {
mDragHelper.captureChildView(mMainView, pointerId);
}
}
/**
* Set the drawer edge (i.e. how sensitive the drawer is) Based on a given screen width
* percentage.
*
* @param displayWidthPercentage larger the value, the more sensitive the drawer swipe is;
* percentage of screen width
* @param drawerLayout drawerLayout to adjust the swipe edge
*/
public static void setDrawerEdge(Activity activity, final float displayWidthPercentage,
DrawerLayout drawerLayout) {
try {
Field mDragger =
drawerLayout.getClass().getSuperclass().getDeclaredField("mLeftDragger");
mDragger.setAccessible(true);
ViewDragHelper leftDragger = (ViewDragHelper) mDragger.get(drawerLayout);
Field mEdgeSize = leftDragger.getClass().getDeclaredField("mEdgeSize");
mEdgeSize.setAccessible(true);
final int currentEdgeSize = mEdgeSize.getInt(leftDragger);
Point displaySize = new Point();
activity.getWindowManager().getDefaultDisplay().getSize(displaySize);
mEdgeSize.setInt(leftDragger,
Math.max(currentEdgeSize, (int) (displaySize.x * displayWidthPercentage)));
} catch (Exception e) {
LogUtil.e(e + ": Exception thrown while changing navdrawer edge size");
}
}
public ToolbarPanelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.getTheme().obtainStyledAttributes(attrs,
R.styleable.ToolbarPanelLayout, 0, 0);
try {
toolbarId = a.getResourceId(R.styleable.ToolbarPanelLayout_pullableToolbarId, -1);
panelId = a.getResourceId(R.styleable.ToolbarPanelLayout_panelId, -1);
} finally {
a.recycle();
}
if (toolbarId == -1 && panelId == -1) {
throw new IllegalStateException("Need to specify toolbarId and panelId");
}
Resources resources = getResources();
final float density = resources.getDisplayMetrics().density;
shadowDrawable = resources.getDrawable(R.drawable.drop_shadow);
setWillNotDraw(false);
dragHelper = ViewDragHelper.create(this, 1.2f, new DragHelperCallback());
dragHelper.setMinVelocity(MIN_FLING_VELOCITY * density);
}
public SlidingUpPanelLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.SlidingUpPanelLayout, defStyle, 0);
isSlidingEnabled = !a.getBoolean(R.styleable.SlidingUpPanelLayout_spl_disableSliding, false);
mExpandThreshold = a.getFloat(R.styleable.SlidingUpPanelLayout_spl_expandThreshold, 0.0f);
mCollapseThreshold = a.getFloat(R.styleable.SlidingUpPanelLayout_spl_collapseThreshold, 0.7f);
a.recycle();
if (isInEditMode()) {
mDragHelper = null;
return;
}
mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
mDragHelper.setMinVelocity(DEFAULT_MIN_FLING_VELOCITY * getResources().getDisplayMetrics().density);
}
@Override
public void onViewDragStateChanged(int state) {
int anchoredTop = (int)(mAnchorPoint*mSlideRange);
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
if (mSlideOffset == 0) {
if (mSlideState != SlideState.EXPANDED) {
updateObscuredViewVisibility();
dispatchOnPanelExpanded(mSlideableView);
mSlideState = SlideState.EXPANDED;
}
} else if (mSlideOffset == (float)anchoredTop/(float)mSlideRange) {
if (mSlideState != SlideState.ANCHORED) {
updateObscuredViewVisibility();
dispatchOnPanelAnchored(mSlideableView);
mSlideState = SlideState.ANCHORED;
}
} else if (mSlideState != SlideState.COLLAPSED) {
dispatchOnPanelCollapsed(mSlideableView);
mSlideState = SlideState.COLLAPSED;
}
}
}
public ZSwipeItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ZSwipeItem);
// 默认是右边缘检测
int ordinal = a.getInt(R.styleable.ZSwipeItem_drag_edge,
DragEdge.Right.ordinal());
mDragEdge = DragEdge.values()[ordinal];
// 默认模式是拉出
ordinal = a.getInt(R.styleable.ZSwipeItem_show_mode,
ShowMode.PullOut.ordinal());
mShowMode = ShowMode.values()[ordinal];
mHorizontalSwipeOffset = a.getDimension(
R.styleable.ZSwipeItem_horizontalSwipeOffset, 0);
mVerticalSwipeOffset = a.getDimension(
R.styleable.ZSwipeItem_verticalSwipeOffset, 0);
a.recycle();
}
/**
* 构造方法
*/
public SlideFrameLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setWillNotDraw(false);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewCompat.setImportantForAccessibility(this, ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
mDragHelper = ViewDragHelper.create(this, 0.5f, new DragHelperCallback());
mDragHelper.setMinVelocity(dip2px(MIN_FLING_VELOCITY));
setEdgeSize(dip2px(20));
mPreviousSnapshotView = new PreviewView(context);
addView(mPreviousSnapshotView, new LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT));
}
@Override
public void onViewDragStateChanged(int state) {
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
mSlideOffset = computeSlideOffset(mSlideableView.getTop());
if (mSlideOffset == 1) {
if (mSlideState != SlideState.EXPANDED) {
updateObscuredViewVisibility();
mSlideState = SlideState.EXPANDED;
dispatchOnPanelExpanded(mSlideableView);
}
} else if (mSlideOffset == 0) {
if (mSlideState != SlideState.COLLAPSED) {
mSlideState = SlideState.COLLAPSED;
dispatchOnPanelCollapsed(mSlideableView);
}
} else if (mSlideOffset < 0) {
mSlideState = SlideState.HIDDEN;
mSlideableView.setVisibility(View.GONE);
dispatchOnPanelHidden(mSlideableView);
}
}
}
public ZSwipeItem(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.ZSwipeItem);
// 默认是右边缘检测
int ordinal = a.getInt(R.styleable.ZSwipeItem_drag_edge,
DragEdge.Right.ordinal());
mDragEdge = DragEdge.values()[ordinal];
// 默认模式是拉出
ordinal = a.getInt(R.styleable.ZSwipeItem_show_mode,
ShowMode.PullOut.ordinal());
mShowMode = ShowMode.values()[ordinal];
mHorizontalSwipeOffset = a.getDimension(
R.styleable.ZSwipeItem_horizontalSwipeOffset, 0);
mVerticalSwipeOffset = a.getDimension(
R.styleable.ZSwipeItem_verticalSwipeOffset, 0);
a.recycle();
}
private void init(Context context, AttributeSet attrs) {
if (attrs != null && context != null) {
TypedArray a = context.getTheme().obtainStyledAttributes(
attrs,
R.styleable.SwipeRevealLayout,
0, 0
);
mDragEdge = a.getInteger(R.styleable.SwipeRevealLayout_dragEdge, DRAG_EDGE_LEFT);
mMinFlingVelocity = a.getInteger(R.styleable.SwipeRevealLayout_flingVelocity, DEFAULT_MIN_FLING_VELOCITY);
mMode = a.getInteger(R.styleable.SwipeRevealLayout_mode, MODE_NORMAL);
mMinDistRequestDisallowParent = a.getDimensionPixelSize(
R.styleable.SwipeRevealLayout_minDistRequestDisallowParent,
dpToPx(DEFAULT_MIN_DIST_REQUEST_DISALLOW_PARENT)
);
}
mDragHelper = ViewDragHelper.create(this, 1.0f, mDragHelperCallback);
mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_ALL);
mGestureDetector = new GestureDetectorCompat(context, mGestureListener);
}
public MarginDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final float density = getResources().getDisplayMetrics().density;
mMinDrawerMargin = (int) (MIN_DRAWER_MARGIN * density + 0.5f);
final float minVel = MIN_FLING_VELOCITY * density;
mLeftCallback = new ViewDragCallback(Gravity.LEFT);
mRightCallback = new ViewDragCallback(Gravity.RIGHT);
mLeftDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mLeftCallback);
mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
mLeftDragger.setMinVelocity(minVel);
mLeftCallback.setDragger(mLeftDragger);
mRightDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mRightCallback);
mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
mRightDragger.setMinVelocity(minVel);
mRightCallback.setDragger(mRightDragger);
// So that we can catch the back button
setFocusableInTouchMode(true);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewGroupCompat.setMotionEventSplittingEnabled(this, false);
}
@Override
public void onViewDragStateChanged(int state) {
int anchoredTop = (int)(mAnchorPoint*mSlideRange);
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
if (mSlideOffset == 0) {
if (mSlideState != SlideState.EXPANDED) {
updateObscuredViewVisibility();
dispatchOnPanelExpanded(mSlideableView);
mSlideState = SlideState.EXPANDED;
}
} else if (mSlideOffset == (float)anchoredTop/(float)mSlideRange) {
if (mSlideState != SlideState.ANCHORED) {
updateObscuredViewVisibility();
dispatchOnPanelAnchored(mSlideableView);
mSlideState = SlideState.ANCHORED;
}
} else if (mSlideState != SlideState.COLLAPSED) {
dispatchOnPanelCollapsed(mSlideableView);
mSlideState = SlideState.COLLAPSED;
}
}
}
@Override
public void onViewDragStateChanged(int state) {
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
if (mSlideOffset == 0) {
updateObscuredViewsVisibility(mSlideableView);
dispatchOnPanelClosed(mSlideableView);
mPreservedOpenState = false;
} else {
dispatchOnPanelOpened(mSlideableView);
mPreservedOpenState = true;
}
}
}
public SwipeItem(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
// scroll threshold
ViewConfiguration vc = ViewConfiguration.get(this.getContext());
mTouchSlop = vc.getScaledTouchSlop();
mDragHelper = ViewDragHelper.create(this, new DragHelperCallback());
}
public ResideLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
final float density = context.getResources().getDisplayMetrics().density;
mOverhangSize = (int) (DEFAULT_OVERHANG_SIZE * density + 0.5f);
setWillNotDraw(false);
mDragHelper = ViewDragHelper.create(this, 0.5f, new DragHelperCallback());
mDragHelper.setMinVelocity(MIN_FLING_VELOCITY * density);
}
@Override
public void onViewDragStateChanged(int state) {
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
mSlideOffset = computeSlideOffset(mSlideableView.getTop());
if (mSlideOffset == 1) {
if (mSlideState != SlideState.EXPANDED) {
updateObscuredViewVisibility();
mSlideState = SlideState.EXPANDED;
dispatchOnPanelExpanded(mSlideableView);
}
} else if (mSlideOffset == 0) {
if (mSlideState != SlideState.COLLAPSED) {
mSlideState = SlideState.COLLAPSED;
dispatchOnPanelCollapsed(mSlideableView);
}
} else if (mSlideOffset < 0) {
mSlideState = SlideState.HIDDEN;
mSlideableView.setVisibility(View.GONE);
dispatchOnPanelHidden(mSlideableView);
} else if (mSlideState != SlideState.ANCHORED) {
updateObscuredViewVisibility();
mSlideState = SlideState.ANCHORED;
dispatchOnPanelAnchored(mSlideableView);
}
}
}
/**
* Enable or disable interaction with the given drawer.
*
* <p>This allows the application to restrict the user's ability to open or close
* the given drawer. DrawerLayout will still respond to calls to {@link #openDrawer(int)},
* {@link #closeDrawer(int)} and friends if a drawer is locked.</p>
*
* <p>Locking a drawer open or closed will implicitly open or close
* that drawer as appropriate.</p>
*
* @param lockMode The new lock mode for the given drawer. One of {@link #LOCK_MODE_UNLOCKED},
* {@link #LOCK_MODE_LOCKED_CLOSED} or {@link #LOCK_MODE_LOCKED_OPEN}.
* @param edgeGravity Gravity.LEFT, RIGHT, START or END.
* Expresses which drawer to change the mode for.
*
* @see #LOCK_MODE_UNLOCKED
* @see #LOCK_MODE_LOCKED_CLOSED
* @see #LOCK_MODE_LOCKED_OPEN
*/
public void setDrawerLockMode(int lockMode, int edgeGravity) {
final int absGrav = GravityCompat.getAbsoluteGravity(edgeGravity,
ViewCompat.getLayoutDirection(this));
if (absGrav == Gravity.LEFT) {
mLockModeLeft = lockMode;
} else if (absGrav == Gravity.RIGHT) {
mLockModeRight = lockMode;
}
if (lockMode != LOCK_MODE_UNLOCKED) {
// Cancel interaction in progress
final ViewDragHelper helper = absGrav == Gravity.LEFT ? mLeftDragger : mRightDragger;
helper.cancel();
}
switch (lockMode) {
case LOCK_MODE_LOCKED_OPEN:
final View toOpen = findDrawerWithGravity(absGrav);
if (toOpen != null) {
openDrawer(toOpen);
}
break;
case LOCK_MODE_LOCKED_CLOSED:
final View toClose = findDrawerWithGravity(absGrav);
if (toClose != null) {
closeDrawer(toClose);
}
break;
// default: do nothing
}
}
@Override
public void onViewDragStateChanged(int state) {
super.onViewDragStateChanged(state);
final int prevState = mState;
switch (state) {
case ViewDragHelper.STATE_DRAGGING:
mState = STATE_DRAGGING;
break;
case ViewDragHelper.STATE_IDLE:
// drag edge is left or right
if (mDragEdge == DRAG_EDGE_LEFT || mDragEdge == DRAG_EDGE_RIGHT) {
if (mMainView.getLeft() == mRectMainClose.left) {
mState = STATE_CLOSE;
} else {
mState = STATE_OPEN;
}
}
// drag edge is top or bottom
else {
if (mMainView.getTop() == mRectMainClose.top) {
mState = STATE_CLOSE;
} else {
mState = STATE_OPEN;
}
}
break;
}
if (mDragStateChangeListener != null && !mAborted && prevState != mState) {
mDragStateChangeListener.onDragStateChanged(mState);
}
}
protected void onSlideStateChanged(int state) {
if(getWindowIsTranslucent()){
return;
}
if (state == ViewDragHelper.STATE_DRAGGING) {
Drawable windowBackground = getWindowBackground();
if (windowBackground != null) {
getWindow().setBackgroundDrawable(windowBackground);
} else {
getWindow().setBackgroundDrawable(getDefaultWindowBackground());
}
}
}
@Override
public boolean onLayoutChild(CoordinatorLayout parent, V child, int layoutDirection) {
if (ViewCompat.getFitsSystemWindows(parent) && !ViewCompat.getFitsSystemWindows(child)) {
ViewCompat.setFitsSystemWindows(child, true);
}
int savedTop = child.getTop();
// First let the parent lay it out
parent.onLayoutChild(child, layoutDirection);
// Offset the bottom sheet
mParentHeight = parent.getHeight();
int peekHeight;
if (mPeekHeightAuto) {
if (mPeekHeightMin == 0) {
mPeekHeightMin = parent.getResources().getDimensionPixelSize(
R.dimen.design_bottom_sheet_peek_height_min);
}
peekHeight = Math.max(mPeekHeightMin, mParentHeight - parent.getWidth() * 9 / 16);
} else {
peekHeight = mPeekHeight;
}
mMinOffset = Math.max(0, mParentHeight - child.getHeight());
mMaxOffset = Math.max(mParentHeight - peekHeight, mMinOffset);
if (mState == STATE_EXPANDED) {
ViewCompat.offsetTopAndBottom(child, mMinOffset);
} else if (mHideable && mState == STATE_HIDDEN) {
ViewCompat.offsetTopAndBottom(child, mParentHeight);
} else if (mState == STATE_COLLAPSED) {
ViewCompat.offsetTopAndBottom(child, mMaxOffset);
} else if (mState == STATE_DRAGGING || mState == STATE_SETTLING) {
ViewCompat.offsetTopAndBottom(child, savedTop - child.getTop());
}
if (mViewDragHelper == null) {
mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
}
mViewRef = new WeakReference<>(child);
mNestedScrollingChildRef = new WeakReference<>(findScrollingChild(child));
return true;
}
@Override
public void onViewDragStateChanged(int state) {
if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
mSlideOffset = computeSlideOffset(mSlideableView.getTop());
if (mSlideOffset == 1) {
if (mSlideState != SlideState.EXPANDED) {
updateObscuredViewVisibility();
mSlideState = SlideState.EXPANDED;
dispatchOnPanelExpanded(mSlideableView);
}
} else if (mSlideOffset == 0) {
if (mSlideState != SlideState.COLLAPSED) {
mSlideState = SlideState.COLLAPSED;
dispatchOnPanelCollapsed(mSlideableView);
}
} else if (mSlideOffset < 0) {
mSlideState = SlideState.HIDDEN;
mSlideableView.setVisibility(View.GONE);
dispatchOnPanelHidden(mSlideableView);
} else if (mSlideState != SlideState.ANCHORED) {
updateObscuredViewVisibility();
mSlideState = SlideState.ANCHORED;
dispatchOnPanelAnchored(mSlideableView);
}
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
int widthMode = MeasureSpec.getMode(widthMeasureSpec);
int widthSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
}
int heightMode = MeasureSpec.getMode(heightMeasureSpec);
int heightSize = MeasureSpec.getSize(heightMeasureSpec);
if (heightMode != MeasureSpec.EXACTLY) {
throw new IllegalStateException("Width must have an exact value or MATCH_PARENT");
}
final int childCount = getChildCount();
// We'll find the current one below.
mSlideableView = null;
for (int i = 0; i < childCount; i++) {
final View child = getChildAt(i);
final LayoutParams lp = (LayoutParams) child.getLayoutParams();
int childWidthSpec = MeasureSpec.makeMeasureSpec(widthSize, MeasureSpec.EXACTLY);
int childHeightSpec = MeasureSpec.makeMeasureSpec(heightSize, MeasureSpec.EXACTLY);
child.measure(childWidthSpec, childHeightSpec);
lp.slideable = true;
mSlideableView = child;
}
setMeasuredDimension(widthSize, heightSize);
mCanSlide = true;
if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE) {
// Cancel scrolling in progress, it's no longer relevant.
mDragHelper.abort();
}
}
public TranslucentDrawerLayout(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
setDescendantFocusability(ViewGroup.FOCUS_AFTER_DESCENDANTS);
final float density = getResources().getDisplayMetrics().density;
mMinDrawerMargin = (int) (MIN_DRAWER_MARGIN * density + 0.5f);
final float minVel = MIN_FLING_VELOCITY * density;
mLeftCallback = new ViewDragCallback(Gravity.LEFT);
mRightCallback = new ViewDragCallback(Gravity.RIGHT);
mLeftDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mLeftCallback);
mLeftDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
mLeftDragger.setMinVelocity(minVel);
mLeftCallback.setDragger(mLeftDragger);
mRightDragger = ViewDragHelper.create(this, TOUCH_SLOP_SENSITIVITY, mRightCallback);
mRightDragger.setEdgeTrackingEnabled(ViewDragHelper.EDGE_RIGHT);
mRightDragger.setMinVelocity(minVel);
mRightCallback.setDragger(mRightDragger);
// So that we can catch the back button
setFocusableInTouchMode(true);
ViewCompat.setImportantForAccessibility(this,
ViewCompat.IMPORTANT_FOR_ACCESSIBILITY_YES);
ViewCompat.setAccessibilityDelegate(this, new AccessibilityDelegate());
ViewGroupCompat.setMotionEventSplittingEnabled(this, false);
if (ViewCompat.getFitsSystemWindows(this)) {
IMPL.configureApplyInsets(this);
mStatusBarBackground = IMPL.getDefaultStatusBarBackground(context);
}
mDrawerElevation = DRAWER_ELEVATION * density;
mNonDrawerViews = new ArrayList<View>();
}
@Override
public void onViewDragStateChanged(int state) {
if (state == draggingState) return;
if ((draggingState == ViewDragHelper.STATE_DRAGGING || draggingState == ViewDragHelper.STATE_SETTLING) &&
state == ViewDragHelper.STATE_IDLE) {
// the view stopped from moving.
if (draggingOffset == getDragRange()) {
onFinishListener.onFinishState();
}
}
draggingState = state;
}
boolean onInterceptTouchEvent(MotionEvent ev) {
boolean interceptForDrag = dragger.shouldInterceptTouchEvent(ev);
boolean interceptForTap = false;
switch (MotionEventCompat.getActionMasked(ev)) {
case MotionEvent.ACTION_DOWN: {
float x = ev.getX();
float y = ev.getY();
initialMotionX = x;
initialMotionY = y;
if (onScreen > 0) {
View child = dragger.findTopChildUnder((int) x, (int) y);
if (child != null && isContentView(child)) {
interceptForTap = true;
}
}
childrenCanceledTouch = false;
break;
}
case MotionEvent.ACTION_MOVE: {
// If we cross the touch slop, don't perform the delayed peek for an edge touch.
if (dragger.checkTouchSlop(ViewDragHelper.DIRECTION_ALL)) {
removeCallbacks();
}
break;
}
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP: {
closeDrawers(true);
childrenCanceledTouch = false;
break;
}
}
return interceptForDrag || interceptForTap || isPeeking || childrenCanceledTouch;
}
public SwipeLayoutConv(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
mDragHelper = ViewDragHelper.create(this, mDragHelperCallback);
TypedArray a = context.obtainStyledAttributes(attrs,
R.styleable.SwipeLayout);
int ordinal = a.getInt(R.styleable.SwipeLayout_drag_edge,
DragEdge.Right.ordinal());
mDragEdge = DragEdge.values()[ordinal];
ordinal = a.getInt(R.styleable.SwipeLayout_show_mode,
ShowMode.PullOut.ordinal());
mShowMode = ShowMode.values()[ordinal];
}
private void reset() {
mActivePointerId = ViewDragHelper.INVALID_POINTER;
if (mVelocityTracker != null) {
mVelocityTracker.recycle();
mVelocityTracker = null;
}
}
public DragLayout(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mGestureDetector = new GestureDetector(context, new SimpleOnGestureListener() {
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
// 如果水平方向的偏移量大于竖直方向的偏移量,就消化该事件,
return Math.abs(distanceX) > Math.abs(distanceY) || super.onScroll(e1, e2, distanceX, distanceY);
}
});
// 永远要记得,该工具实现的拖拉是基于控件位置的改变来实现的
mViewDragHelper = ViewDragHelper.create(this, new DragViewCallBack());
}