androidx.customview.widget.ViewDragHelper Java Examples

The following examples show how to use androidx.customview.widget.ViewDragHelper. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: Service.java    From Pocket-Plays-for-Twitch with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method for increasing a Navigation Drawer's edge size.
 */
public static void increaseNavigationDrawerEdge(DrawerLayout aDrawerLayout, Context context) {
    // Increase the area from which you can open the navigation drawer.
    try {
        Field mDragger = aDrawerLayout.getClass().getDeclaredField("mLeftDragger");
        mDragger.setAccessible(true);
        ViewDragHelper draggerObj = (ViewDragHelper) mDragger.get(aDrawerLayout);

        Field mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
        mEdgeSize.setAccessible(true);
        int edgeSize = mEdgeSize.getInt(draggerObj) * 3;

        mEdgeSize.setInt(draggerObj, edgeSize); //optimal value as for me, you may set any constant in dp
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #2
Source File: PhotoDrawerLayout.java    From CrazyDaily with Apache License 2.0 6 votes vote down vote up
/**
 * 设置拖动范围
 *
 * @param px 单位像素
 */
private void setEdgeSize(int px) {
    try {
        Field leftDragger = getClass().getSuperclass().getDeclaredField(
                "mLeftDragger");
        leftDragger.setAccessible(true);
        ViewDragHelper viewDragHelper = (ViewDragHelper) leftDragger
                .get(this);
        Field edgeSize = viewDragHelper.getClass().getDeclaredField(
                "mEdgeSize");
        edgeSize.setAccessible(true);
        edgeSize.setInt(viewDragHelper, px);
    } catch (NoSuchFieldException | IllegalAccessException e) {
        e.printStackTrace();
    }
}
 
Example #3
Source File: Service.java    From Twire with GNU General Public License v3.0 6 votes vote down vote up
/**
 * Method for increasing a Navigation Drawer's edge size.
 */
public static void increaseNavigationDrawerEdge(DrawerLayout aDrawerLayout, Context context) {
    // Increase the area from which you can open the navigation drawer.
    try {
        Field mDragger = aDrawerLayout.getClass().getDeclaredField("mLeftDragger");
        mDragger.setAccessible(true);
        ViewDragHelper draggerObj = (ViewDragHelper) mDragger.get(aDrawerLayout);

        Field mEdgeSize = draggerObj.getClass().getDeclaredField("mEdgeSize");
        mEdgeSize.setAccessible(true);
        int edgeSize = mEdgeSize.getInt(draggerObj) * 3;

        mEdgeSize.setInt(draggerObj, edgeSize); //optimal value as for me, you may set any constant in dp
    } catch (Exception e) {
        e.printStackTrace();
    }
}
 
Example #4
Source File: MainDragLayout.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
@Override
        public boolean onInterceptTouchEvent(MotionEvent ev) {
//                如果符合条件直接截获事件由自己在onTouchEvent方法中处理
//                如果在释放自动滑动的过程中点击,会导致停止动画
                boolean result = mViewDragHelper.shouldInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
//                这里是为了防止在滑动的过程中,突然点击,导致被点击的页面停止滑动的的情况发生
                if (!result && mViewDragHelper.getViewDragState() == ViewDragHelper.STATE_DRAGGING && ev.getAction() == MotionEvent.ACTION_UP) {
                        if (getChildAt(2).getLeft() < range / 2) {
                                closeMenu();
                        } else {
                                openMenu();
                        }
                }


                return result;
        }
 
Example #5
Source File: SwipeBackLayout.java    From AndroidNavigation with MIT License 6 votes vote down vote up
@Override
protected boolean drawChild(Canvas canvas, View child, long drawingTime) {
    final boolean drawContent = child == mCapturedView;
    int index = indexOfChild(child);
    if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && index == getChildCount() - 2) {
        View lastChild = getChildAt(getChildCount() - 1);
        canvas.save();
        canvas.clipRect(0, 0, lastChild.getLeft(), getHeight());
    }

    boolean ret = super.drawChild(canvas, child, drawingTime);

    if (mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE && index == getChildCount() - 2) {
        canvas.restore();
    }

    if (mTabBar != null && drawContent) {
        drawTabBar(canvas, child);
    }

    if (mScrimOpacity > 0 && drawContent
            && mDragHelper.getViewDragState() != ViewDragHelper.STATE_IDLE) {
        drawScrim(canvas, child);
    }
    return ret;
}
 
Example #6
Source File: DragLayout.java    From NewFastFrame with Apache License 2.0 6 votes vote down vote up
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());
    }
 
Example #7
Source File: NowPlayingView.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
/**
 * Called when a layout is requested from the graphics system.
 *
 * @param changed If the layout is changed (size, ...)
 * @param l       Left position
 * @param t       Top position
 * @param r       Right position
 * @param b       Bottom position
 */
@Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
    // Calculate the maximal range that the view is allowed to be dragged
    mDragRange = (getMeasuredHeight() - mHeaderView.getMeasuredHeight());

    // New temporary top position, to fix the view at top or bottom later if state is idle.
    int newTop = mTopPosition;

    // fix height at top or bottom if state idle
    if (mDragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
        newTop = (int) (mDragRange * mDragOffset);
    }

    // Request the upper part of the NowPlayingView (header)
    mHeaderView.layout(
            0,
            newTop,
            r,
            newTop + mHeaderView.getMeasuredHeight());

    // Request the lower part of the NowPlayingView (main part)
    mMainView.layout(
            0,
            newTop + mHeaderView.getMeasuredHeight(),
            r,
            newTop + b);
}
 
Example #8
Source File: DuoDrawerLayout.java    From duo-navigation-drawer with Apache License 2.0 5 votes vote down vote up
private void initialize() {
    mLayoutInflater = LayoutInflater.from(getContext());
    mViewDragCallback = new ViewDragCallback();
    mViewDragHelper = ViewDragHelper.create(this, 1.0f, mViewDragCallback);
    mViewDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);

    this.setFocusableInTouchMode(true);
    this.setClipChildren(false);
    this.requestFocus();
}
 
Example #9
Source File: DuoDrawerLayout.java    From duo-navigation-drawer with Apache License 2.0 5 votes vote down vote up
@Override
public void onEdgeDragStarted(int edgeFlags, int pointerId) {
    mIsEdgeDrag = true;

    if (tryCaptureView(mContentView, pointerId) && edgeFlags == ViewDragHelper.EDGE_LEFT) {
        mViewDragHelper.captureChildView(mContentView, pointerId);
    }
}
 
Example #10
Source File: BottomSheetBehaviorGoogleMapsLike.java    From CustomBottomSheetBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onLayoutChild( CoordinatorLayout parent, V child, int layoutDirection ) {
    // First let the parent lay it out
    if (mState != STATE_DRAGGING && mState != STATE_SETTLING) {
        if (parent.getFitsSystemWindows() &&
                !child.getFitsSystemWindows()) {
            child.setFitsSystemWindows(true);
        }
        parent.onLayoutChild(child, layoutDirection);
    }
    // Offset the bottom sheet
    mParentHeight = parent.getHeight();
    mMinOffset = Math.max(0, mParentHeight - child.getHeight());
    mMaxOffset = Math.max(mParentHeight - mPeekHeight, mMinOffset);

    /**
     * New behavior
     */
    if (mState == STATE_ANCHOR_POINT) {
        ViewCompat.offsetTopAndBottom(child, mAnchorPoint);
    } else 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);
    }
    if ( mViewDragHelper == null ) {
        mViewDragHelper = ViewDragHelper.create( parent, mDragCallback );
    }
    mViewRef = new WeakReference<>(child);
    mNestedScrollingChildRef = new WeakReference<>( findScrollingChild( child ) );
    return true;
}
 
Example #11
Source File: BottomSheetBehaviorGoogleMapsLike.java    From CustomBottomSheetBehavior with Apache License 2.0 5 votes vote down vote up
@Override
public boolean onTouchEvent( CoordinatorLayout parent, V child, MotionEvent event ) {
    if ( ! child.isShown() ) {
        return false;
    }

    int action = event.getActionMasked();
    if ( mState == STATE_DRAGGING  &&  action == MotionEvent.ACTION_DOWN ) {
        return true;
    }

    // Detect scroll direction for ignoring collapsible
    if(mLastStableState == STATE_ANCHOR_POINT && action==MotionEvent.ACTION_MOVE) {
        if(event.getY()>mInitialY && !mCollapsible) {
            reset();
            return false;
        }
    }
  
    if (mViewDragHelper == null) {
        mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
    }
    
    mViewDragHelper.processTouchEvent(event);
    
    if ( action == MotionEvent.ACTION_DOWN ) {
        reset();
    }

    // The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
    // to capture the bottom sheet in case it is not captured and the touch slop is passed.
    if ( action == MotionEvent.ACTION_MOVE  &&  ! mIgnoreEvents ) {
        if ( Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop() ) {
            mViewDragHelper.captureChildView( child, event.getPointerId(event.getActionIndex()) );
        }
    }
    return ! mIgnoreEvents;
}
 
Example #12
Source File: SwipeBackLayout.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
public SwipeBackLayout(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    setWillNotDraw(false);
    mDragHelper = ViewDragHelper.create(this, 1f, new DragHelperCallback());
    mTouchSlop = mDragHelper.getTouchSlop();
    setSwipeBackListener(defaultSwipeBackListener);

    init(context, attrs);
}
 
Example #13
Source File: SwipeBackLayout.java    From AndroidAnimationExercise with Apache License 2.0 5 votes vote down vote up
@Override
public void onViewDragStateChanged(int state) {
    super.onViewDragStateChanged(state);
    if (state == ViewDragHelper.STATE_IDLE) {
        if (mSwipeBackListener != null) {
            if (swipeBackFraction == 0) {
                mSwipeBackListener.onViewSwipeFinished(mDragContentView, false);
            } else if (swipeBackFraction == 1) {
                mSwipeBackListener.onViewSwipeFinished(mDragContentView, true);
            }
        }
    }
}
 
Example #14
Source File: NowPlayingView.java    From odyssey with GNU General Public License v3.0 5 votes vote down vote up
public NowPlayingView(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs, defStyle);
    mDragHelper = ViewDragHelper.create(this, 1f, new BottomDragCallbackHelper());
    mPlaybackServiceState = PlaybackService.PLAYSTATE.STOPPED;

    mLastTrack = new TrackModel();

    mServiceConnection = new PlaybackServiceConnection(getContext().getApplicationContext());
    mServiceConnection.setNotifier(new ServiceConnectionListener());
}
 
Example #15
Source File: PullDismissLayout.java    From StoryView with MIT License 5 votes vote down vote up
public boolean onInterceptTouchEvent(MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    boolean pullingDown = false;

    switch (action) {
        case MotionEvent.ACTION_DOWN:
            verticalTouchSlop = event.getY();
        case MotionEvent.ACTION_MOVE:
            final float dy = event.getY() - verticalTouchSlop;
            if (dy > dragHelper.getTouchSlop()) {
                pullingDown = true;
                mTouchCallbacks.touchPull();
            }else{
                mTouchCallbacks.touchDown(event.getX(), event.getY());
            }
            break;
        case MotionEvent.ACTION_UP:
        case MotionEvent.ACTION_CANCEL:
            verticalTouchSlop = 0.0f;
            mTouchCallbacks.touchUp();
            break;
    }

    if (!dragHelper.shouldInterceptTouchEvent(event) && pullingDown) {
        if (dragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE &&
                dragHelper.checkTouchSlop(ViewDragHelper.DIRECTION_VERTICAL)) {

            View child = getChildAt(0);
            if (child != null && !listener.onShouldInterceptTouchEvent()) {
                dragHelper.captureChildView(child, event.getPointerId(0));
                return dragHelper.getViewDragState() == ViewDragHelper.STATE_DRAGGING;
            }
        }
    }
    return false;
}
 
Example #16
Source File: BottomSheetBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void reset() {
  activePointerId = ViewDragHelper.INVALID_POINTER;
  if (velocityTracker != null) {
    velocityTracker.recycle();
    velocityTracker = null;
  }
}
 
Example #17
Source File: SwipeDismissBehavior.java    From material-components-android with Apache License 2.0 5 votes vote down vote up
private void ensureViewDragHelper(ViewGroup parent) {
  if (viewDragHelper == null) {
    viewDragHelper =
        sensitivitySet
            ? ViewDragHelper.create(parent, sensitivity, dragCallback)
            : ViewDragHelper.create(parent, dragCallback);
  }
}
 
Example #18
Source File: BottomSheetBehavior.java    From Mysplash with GNU Lesser General Public License v3.0 5 votes vote down vote up
private void reset() {
    activePointerId = ViewDragHelper.INVALID_POINTER;
    if (velocityTracker != null) {
        velocityTracker.recycle();
        velocityTracker = null;
    }
}
 
Example #19
Source File: QuickAttachmentDrawer.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
public QuickAttachmentDrawer(Context context, AttributeSet attrs, int defStyle) {
  super(context, attrs, defStyle);
  dragHelper = ViewDragHelper.create(this, 1.f, new ViewDragHelperCallback());
  initializeView();
  updateHalfExpandedAnchorPoint();
  onConfigurationChanged();
}
 
Example #20
Source File: QuickAttachmentDrawer.java    From deltachat-android with GNU General Public License v3.0 5 votes vote down vote up
@Override
public void onViewDragStateChanged(int state) {
  if (dragHelper.getViewDragState() == ViewDragHelper.STATE_IDLE) {
    setDrawerState(drawerState);
    slideOffset = getTargetSlideOffset();
    requestLayout();
  }
}
 
Example #21
Source File: ViewDragFrame.java    From zone-sdk with MIT License 5 votes vote down vote up
public void setMoveType(int moveType) {
    if(moveType!=ViewDragHelper.EDGE_LEFT&&moveType!=ViewDragHelper.EDGE_RIGHT)
        throw new IllegalArgumentException("参数异常!");
    mViewDragHelper.setEdgeTrackingEnabled(moveType);
    this.moveType = moveType;

}
 
Example #22
Source File: ViewDragFrame.java    From zone-sdk with MIT License 5 votes vote down vote up
@Override
public int clampViewPositionHorizontal(View child, int left, int dx) {

    int returnValue = 0;
    //通过条件 控制 在什么范围滑动
    if (mMainView == child) {
        log("left:" + left + "\t dx:" + dx);
        switch (moveType) {
            case ViewDragHelper.EDGE_RIGHT:
                if (left >= -MaxMoveLength && left < 0) {
                    returnValue = left;
                } else if (left >= 0) {
                    returnValue = 0;
                } else {
                    returnValue = -MaxMoveLength;
                }
                break;
            case ViewDragHelper.EDGE_LEFT:
                if (left <= MaxMoveLength && left > 0) {
                    returnValue = left;
                } else if (left <= 0) {
                    returnValue = 0;
                } else {
                    returnValue = MaxMoveLength;
                }
                break;
        }
    }
    return returnValue;
}
 
Example #23
Source File: ViewDragFrame.java    From zone-sdk with MIT License 5 votes vote down vote up
public void open() {
    open = true;
    switch (moveType) {
        case ViewDragHelper.EDGE_RIGHT:
            mViewDragHelper.smoothSlideViewTo(mMainView, -MaxMoveLength, 0);
            break;
        case ViewDragHelper.EDGE_LEFT:
            mViewDragHelper.smoothSlideViewTo(mMainView, MaxMoveLength, 0);
            break;
    }

    ViewCompat.postInvalidateOnAnimation(this);//兼容刷新的
}
 
Example #24
Source File: SlideInAttachmentsLayout.java    From toktok-android with GNU General Public License v3.0 5 votes vote down vote up
public SlideInAttachmentsLayout(
        Context context,
        AttributeSet attrs,
        int defStyle
) {
    super(context, attrs, defStyle);
    mDragHelper = ViewDragHelper.create(this, 1f, new DragHelperCallback());
}
 
Example #25
Source File: DragLayout.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
//                如果符合条件直接截获事件由自己在onTouchEvent方法中处理
//                如果在释放自动滑动的过程中点击,会导致停止动画
        if (isIntercept()) {
            return false;
        }
        boolean result = mViewDragHelper.shouldInterceptTouchEvent(ev) && mGestureDetector.onTouchEvent(ev);
//                这里是为了防止在滑动的过程中,突然点击,导致被点击的页面停止滑动的的情况发生

        if (!result && mViewDragHelper.getViewDragState() == ViewDragHelper.STATE_DRAGGING && ev.getAction() == MotionEvent.ACTION_UP) {
            if (mode == LEFT_MODE) {
                if (content.getLeft() < range / 2) {
                    closeMenu();
                } else {
                    openMenu();
                }
            } else {
                if (content.getRight() < range / 2) {
                    openMenu();
                } else {
                    closeMenu();
                }
            }
        }
        return result;
    }
 
Example #26
Source File: MainDragLayout.java    From NewFastFrame with Apache License 2.0 5 votes vote down vote up
public MainDragLayout(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());
        }
 
Example #27
Source File: PullDismissLayout.java    From StoryView with MIT License 5 votes vote down vote up
private void init(@NonNull Context context) {
    if (!isInEditMode()) {
        ViewConfiguration vc = ViewConfiguration.get(context);
        minFlingVelocity = (float) vc.getScaledMinimumFlingVelocity();
        dragHelper = ViewDragHelper.create(this, new PullDismissLayout.ViewDragCallback(this));
    }
}
 
Example #28
Source File: BottomSheetBehavior.java    From bottomsheetrecycler with Apache License 2.0 5 votes vote down vote up
private void reset() {
    activePointerId = ViewDragHelper.INVALID_POINTER;
    if (velocityTracker != null) {
        velocityTracker.recycle();
        velocityTracker = null;
    }
}
 
Example #29
Source File: PullDismissLayout.java    From StoryView with MIT License 5 votes vote down vote up
public void onViewDragStateChanged(int state) {
    if (capturedView != null && dismissed && state == ViewDragHelper.STATE_IDLE) {
        pullDismissLayout.removeView(capturedView);
        if (pullDismissLayout.listener != null) {
            pullDismissLayout.listener.onDismissed();
        }
    }
}
 
Example #30
Source File: SwipeBackLayout.java    From AndroidNavigation with MIT License 5 votes vote down vote up
public SwipeBackLayout(Context context, AttributeSet attrs, int defStyle) {
    super(context, attrs);
    mDragHelper = ViewDragHelper.create(this, new ViewDragCallback());
    final float density = getResources().getDisplayMetrics().density;
    final float minVelocity = MIN_FLING_VELOCITY * density;
    mDragHelper.setMinVelocity(minVelocity);
    mDragHelper.setEdgeTrackingEnabled(ViewDragHelper.EDGE_LEFT);
}