关闭项目的连续屏幕位置(ItemTouchHelper)

时间:2015-09-25 08:34:24

标签: android android-recyclerview drag

我正在使用ItemTouchHelper创建一个可拖动和可转换的回收者视图(详见https://github.com/iPaulPro/Android-ItemTouchHelper-Demo

我已经查看了ItemTouchHelper和ItemTouchHelper.callback的所有方法,并且没有任何方法可以在拖动时为我提供手指或项目的实时屏幕位置。如果有人可以帮助它将是一个救生员!

2 个答案:

答案 0 :(得分:0)

我最终不得不进入Recyclerview V7 jar并从ItemTouchHelper获取代码并创建我自己的ItemTouchHelper版本: 我将自己的方法添加到ItemTouchHelper.Callback

        public abstract void onTouchMovement(MotionEvent touch_event, int directionFlags, int pointerIndex);

我在ItemTouchHelper的updateDxDy方法中调用onTouchMovement:

 public void updateDxDy(MotionEvent ev, int directionFlags, int pointerIndex) {
    final float x = MotionEventCompat.getX(ev, pointerIndex);
    final float y = MotionEventCompat.getY(ev, pointerIndex);

    mCallback.onTouchMovement(ev, directionFlags, pointerIndex);

现在,当我创建一个ItemTouchHelper.Callback时,它附带OnTouchMovement,它在拖动/滑动的每个点提供位置

答案 1 :(得分:0)

您还可以查看ItemTouchHelper的内部OnInterceptTouchEvent方法以获得反馈

 private final RecyclerView.OnItemTouchListener mOnItemTouchListener
        = new RecyclerView.OnItemTouchListener() {
    @Override
    public boolean onInterceptTouchEvent(RecyclerView recyclerView, MotionEvent event) {
        mGestureDetector.onTouchEvent(event);
        if (DEBUG) {
            Log.d(TAG, "intercept: x:" + event.getX() + ",y:" + event.getY() + ", " + event);
        }
        final int action = MotionEventCompat.getActionMasked(event);
        if (action == MotionEvent.ACTION_DOWN) {
            mActivePointerId = MotionEventCompat.getPointerId(event, 0);
            mInitialTouchX = event.getX();
            mInitialTouchY = event.getY();
            obtainVelocityTracker();
            if (mSelected == null) {
                final RecoverAnimation animation = findAnimation(event);
                if (animation != null) {
                    mInitialTouchX -= animation.mX;
                    mInitialTouchY -= animation.mY;
                    endRecoverAnimation(animation.mViewHolder, true);
                    if (mPendingCleanup.remove(animation.mViewHolder.itemView)) {
                        mCallback.clearView(mRecyclerView, animation.mViewHolder);
                    }
                    select(animation.mViewHolder, animation.mActionState);
                    updateDxDy(event, mSelectedFlags, 0);
                }
            }
        } else if (action == MotionEvent.ACTION_CANCEL || action == MotionEvent.ACTION_UP) {
            wdwdwd
            mActivePointerId = ACTIVE_POINTER_ID_NONE;
            select(null, ACTION_STATE_IDLE);
        } else if (mActivePointerId != ACTIVE_POINTER_ID_NONE) {
            // in a non scroll orientation, if distance change is above threshold, we
            // can select the item
            final int index = MotionEventCompat.findPointerIndex(event, mActivePointerId);
            if (DEBUG) {
                Log.d(TAG, "pointer index " + index);
            }
            if (index >= 0) {
                checkSelectForSwipe(action, event, index);
            }
        }
        if (mVelocityTracker != null) {
            mVelocityTracker.addMovement(event);
        }
        return mSelected != null;
    }