ItemTouchHelper实现部分响应拖动

/**
     * Starts dragging the provided ViewHolder. By default, ItemTouchHelper starts a drag when a
     * View is long pressed. You can disable that behavior by overriding
     * {@link ItemTouchHelper.Callback#isLongPressDragEnabled()}.
     拖动一个ItemTouchHelper绑定的RV的时候,默认实现是拖动,你可以通过重写Callback#isLongPressDragEnabled()
     方法来禁用这种手势响应
     * 

* For this method to work: 下面几条是startDrag的使用前提 *

    *
  • The provided ViewHolder must be a child of the RecyclerView to which this * ItemTouchHelper 首先:入参vh必须是对应绑定ItemTouchHelper的RV,额~ 废话 * is attached.
  • *
  • {@link ItemTouchHelper.Callback} must have dragging enabled.
  • 其次:CallBack必须可拖拉? *
  • There must be a previous touch event that was reported to the ItemTouchHelper * through RecyclerView's ItemTouchListener mechanism. As long as no other ItemTouchListener * grabs previous events, this should work as expected.
  • 最后: touch时间必须能正确得汇报至ItemTouchHelper。也就是说要能正确进入mItemTouchListener的 onInterceptTouchEvent方法等,确保没有其他RV关联的ItemTouchListener抢占了之前的时间,这方法 就能正确工作了 * * For example, if you would like to let your user to be able to drag an Item by touching one * of its descendants, you may implement it as follows: 想让你item里的一部分响应点击? OK 按下面这么重写 *
         *     viewHolder.dragButton.setOnTouchListener(new View.OnTouchListener() {
         *         public boolean onTouch(View v, MotionEvent event) {
         *             if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
         *                 mItemTouchHelper.startDrag(viewHolder);
         *             }
         *             return false;
         *         }
         *     });
         * 
    *

    * * @param viewHolder The ViewHolder to start dragging. It must be a direct child of * RecyclerView. * @see ItemTouchHelper.Callback#isItemViewSwipeEnabled() */ public void startDrag(ViewHolder viewHolder) { if (!mCallback.hasDragFlag(mRecyclerView, viewHolder)) { Log.e(TAG, "Start drag has been called but swiping is not enabled"); return; } if (viewHolder.itemView.getParent() != mRecyclerView) { Log.e(TAG, "Start drag has been called with a view holder which is not a child of " + "the RecyclerView which is controlled by this ItemTouchHelper."); return; } obtainVelocityTracker(); mDx = mDy = 0f; select(viewHolder, ACTION_STATE_DRAG); }


你可能感兴趣的:(Android源码分析)