文章目录
  1. 1. 概述:
    1. 1.1. 本文记录:
    2. 1.2. 1、What is ViewDragHelper ?
      1. 1.2.1. ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.
      2. 1.2.2. ViewDragHelper是一个写自定义ViewGroup的工具类,它提供了一些有用的操作和状态跟踪来让用户对View在其父容器中进行拖拽和重新摆放。
    3. 1.3. 2、How to create it ?
    4. 1.4. 3、Important Callback

概述:

本文记录:

  • ViewDragHelper的基本介绍和使用(初稿)

1、What is ViewDragHelper ?

ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.

ViewDragHelper是一个写自定义ViewGroup的工具类,它提供了一些有用的操作和状态跟踪来让用户对View在其父容器中进行拖拽和重新摆放。

2、How to create it ?

1
2
3
4
5
6
7
8
9
10
11
12
13
/**
* Factory method to create a new ViewDragHelper.//工厂方法创建一个ViewDragHelper
*
* @param forParent Parent view to monitor
* @param sensitivity Multiplier for how sensitive the helper should be about detecting the start of a drag. Larger values are more sensitive. 1.0f is normal.
* @param cb Callback to provide information and receive events//一个提供信息和接收事件的回调
* @return a new ViewDragHelper instance
*/

public static ViewDragHelper create(ViewGroup forParent, float sensitivity, Callback cb) {
final ViewDragHelper helper = create(forParent, cb);
helper.mTouchSlop = (int) (helper.mTouchSlop * (1 / sensitivity));
return helper;
}

3、Important Callback

http://7xqumq.com1.z0.glb.clouddn.com/duwei_viewdraghelper.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18

/**
* Called when the user's input indicates that they want to capture the given child view
* with the pointer indicated by pointerId. The callback should return true if the user
* is permitted to drag the given view with the indicated pointer.
*
* <p>ViewDragHelper may call this method multiple times for the same view even if
* the view is already captured; this indicates that a new pointer is trying to take
* control of the view.</p>
*
* <p>If this method returns true, a call to {@link #onViewCaptured(android.view.View, int)}
* will follow if the capture is successful.</p>
*
* @param child Child the user is attempting to capture
* @param pointerId ID of the pointer attempting the capture
* @return true if capture should be allowed, false otherwise
*/

public abstract boolean tryCaptureView(View child, int pointerId);
1
2
3
4
5
6
7
8
9
10
11
/**
* 约束被拖拽的子View沿着X轴移动,默认的实现不允许水平移动 ,子类必须覆盖这个方法并提供想要的 clamping.
*
* @param child Child view being dragged
* @param left Attempted motion along the X axis
* @param dx Proposed change in position for left
* @return The new clamped position for left
*/

public int clampViewPositionHorizontal(View child, int left, int dx) {
return 0;
}
文章目录
  1. 1. 概述:
    1. 1.1. 本文记录:
    2. 1.2. 1、What is ViewDragHelper ?
      1. 1.2.1. ViewDragHelper is a utility class for writing custom ViewGroups. It offers a number of useful operations and state tracking for allowing a user to drag and reposition views within their parent ViewGroup.
      2. 1.2.2. ViewDragHelper是一个写自定义ViewGroup的工具类,它提供了一些有用的操作和状态跟踪来让用户对View在其父容器中进行拖拽和重新摆放。
    3. 1.3. 2、How to create it ?
    4. 1.4. 3、Important Callback