自定义拖拽效果

自定义拖拽效果_第1张图片
拖拽效果

如图,要想实现上图的效果,需要自定义ViewGroup,自定义ViewGroup的代码如下:


public class DragSlideLayout extends RelativeLayout {

    private View mDragView;
    private ViewDragHelper mDragHelper;

    public DragSlideLayout(Context context) {
        super(context);
        init();
    }

    public DragSlideLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public DragSlideLayout(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mDragHelper = ViewDragHelper.create(this, 1.0f, new DragHelperCallback());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        int childCount = getChildCount();
        mDragView = getChildAt(childCount - 1);
    }

    @Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        final int action = MotionEventCompat.getActionMasked(ev);
        if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            mDragHelper.cancel();
            return false;
        }
        return mDragHelper.shouldInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        mDragHelper.processTouchEvent(ev);
        return true;
    }

    class DragHelperCallback extends ViewDragHelper.Callback {

        @Override
        public boolean tryCaptureView(View child, int pointerId) {
            return child == mDragView;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            final int topBound = getPaddingTop();
            final int bottomBound = getHeight() - mDragView.getHeight() - getPaddingBottom();
            return Math.min(Math.max(top, topBound), bottomBound);
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            final int leftBound = getPaddingLeft();
            final int rightBound = getWidth() - mDragView.getWidth() - getPaddingRight();
            return Math.min(Math.max(left, leftBound), rightBound);
        }

        @Override
        public int getViewVerticalDragRange(View child) {
            return getMeasuredHeight() - child.getMeasuredHeight();
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return getMeasuredWidth() - child.getMeasuredWidth() - getPaddingRight();
        }

        @TargetApi(Build.VERSION_CODES.HONEYCOMB)
        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            super.onViewReleased(releasedChild, xvel, yvel);
            int leftBound = getPaddingLeft();
            int rightBound = getViewHorizontalDragRange(releasedChild);
            int toTarget = releasedChild.getX() > (getMeasuredWidth() / 2 - releasedChild.getMeasuredWidth() / 2) ? rightBound : leftBound;
            mDragHelper.settleCapturedViewAt(toTarget, (int) releasedChild.getY());
            invalidate();
        }
    }

    @Override
    public void computeScroll() {
        if (mDragHelper.continueSettling(true)) {
            invalidate();
        }
    }
}

其中xml的代码如下:





    


你可能感兴趣的:(自定义拖拽效果)