Android slide right hand end of the event distribution instance Activity (b)

Foreword

Benpian above one achieve the same function, ViewDragHelper Cipian before using music inside the lock screen wallpaper function used to achieve the final effect is the same effect on the articles, so just pick Benpian new content to explain.

Specific steps

Event distribution and Conflict Resolution

@Override
    public boolean onInterceptTouchEvent(MotionEvent ev) {
        boolean canScrollHorizontally = canScrollHorizontally(-1, this);
        if (!canScrollHorizontally) {
            return mViewDragHelper.shouldInterceptTouchEvent(ev);
        }
        return super.onInterceptTouchEvent(ev);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        mViewDragHelper.processTouchEvent(event);
        return true;
    }

Description: On an already say clearly an event which contains three small events, because this post is the main event referred ViewDragHelper processing, the processing canScrollHorizontally () method, the determination conditions are not only on the Move event, otherwise Down , Up event ViewDragHelper not receive, view the source code to know mViewDragHelper.shouldInterceptTouchEvent (ev) and mViewDragHelper.processTouchEvent (event) method must be three simultaneous events.

Slide processing

By ViewDragHelper achieve sliding effect, it is to achieve sliding effect by implementing ViewDragHelper callback ViewDragHelper.Callback.

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

        @Override
        public void onViewReleased(View releasedChild, float xvel, float yvel) {
            //当前回调,松开手时触发,比较触发条件和当前的滑动距离
            int left = releasedChild.getLeft();
            if (left <= mMaxSlideWidth) {
                //缓慢滑动的方法,小于触发条件,滚回去
                mViewDragHelper.settleCapturedViewAt(0, 0);
            } else {
                //大于触发条件,滚出去...
                mViewDragHelper.settleCapturedViewAt(mScreenWidth, 0);
            }

            //需要手动调用更新界面的方法
            invalidate();
        }

        @Override
        public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {
            mScrollPercent = Math.abs((float) left
                    / (mScreenWidth + mEdgeShadow.getIntrinsicWidth()));
            //重绘
            invalidate();

            //当滚动位置到达屏幕最右边,则关掉Activity
            if (changedView == mRootView && left >= mScreenWidth) {
                mActivity.finish();
            }
        }

        @Override
        public int getViewHorizontalDragRange(View child) {
            return ViewDragHelper.EDGE_LEFT;
        }

        @Override
        public int clampViewPositionHorizontal(View child, int left, int dx) {
            //限制左右拖拽的位移
            left = left >= 0 ? left : 0;
            return left;
        }

        @Override
        public int clampViewPositionVertical(View child, int top, int dy) {
            //上下不能移动,返回0
            return 0;
        }

        @Override
        public void onEdgeDragStarted(int edgeFlags, int pointerId) {
            //触发边缘时,主动捕捉mRootView
            mViewDragHelper.captureChildView(mRootView, pointerId);
        }

Description: 1, tryCaptureView (): user determines which sub-View captured behavior, since we are moving the entire ViewGroup, direct returns false.
2, onViewReleased (): when a drag completion callback view, view the final position, the interior (0, 0) is achieved mViewDragHelper.settleCapturedViewAt method by calling the mobile after the main processing let go Scroller
3, onViewPositionChanged (): name know methods See View callback when a change in position, it can be determined by the final end position exceeds the right edge Activity
. 4, getViewHorizontalDragRange (): Get the current range in the horizontal direction, the right slider provided to the left edge
5, clampViewPositionHorizontal (): limit level position and direction, the default is not limited, it is necessary to rewrite limit is greater than 0, this can be achieved right slide
6, clampViewPositionVertical (): limit position in the vertical direction, may not be rewritten right slide
7, onEdgeDragStarted (): no subclass capturing, from the trailing edge of the parent class when the callback before setting, this time to capture the view of the activity

@Override
    public void computeScroll() {
        //使用settleCapturedViewAt方法是,必须重写computeScroll方法,传入true
        //持续滚动期间,不断刷新ViewGroup
        if (mViewDragHelper.continueSettling(true))
            ViewCompat.postInvalidateOnAnimation(this);
    }

Description: mViewDragHelper.continueSettling (true) method while dragging the callback return value constantly make the process more smooth drag, echoes mViewDragHelper.settleCapturedViewAt (0, 0), mainly using the sliding mechanism of Scroller.

Touch range

The above steps can be the right slide has been achieved, but ViewDragHelper internal detection is an edge, and then to go drag. The default source is ViewDragHelper edge 20dp, if you want to achieve full-screen drag effect must modify this value, there is no way to view the source code and ViewDragHelper edge width may be modified so that reflective achieved.

/**
     * 设置可以拖拽的触点范围
     *
     * @param touchRange
     */
    private void setTouchRange(int touchRange) {
        Class<?> aClass = mViewDragHelper.getClass();
        Field mDividerHeight = null;
        try {
            mDividerHeight = aClass.getDeclaredField("mEdgeSize");
            mDividerHeight.setAccessible(true);
            mDividerHeight.setInt(mViewDragHelper, touchRange);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

to sum up

Part Benpian using the different ways to modify the main difference is a matter of timing and touch event handling scope, as well as of each callback method ViewDragHelper deal with other aspects no difference, the test has been completed, the effect is entirely consistent with Part I . It is still further package, follow-up will continue to update the latest code.
Right slide end Activity

Reproduced in: https: //www.jianshu.com/p/533d199db90e

Guess you like

Origin blog.csdn.net/weixin_33696106/article/details/91079267