package com.android.ItemTouchHelper.helper;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Rect;
import android.os.Build;
import android.support.annotation.Nullable;
import android.support.v4.view.GestureDetectorCompat;
import android.support.v4.view.ViewCompat;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.RecyclerView.OnItemTouchListener;
import android.support.v7.widget.RecyclerView.ViewHolder;
import android.support.v7.widget.helper.ItemTouchUIUtil;
import android.view.GestureDetector;
import android.view.HapticFeedbackConstants;
import android.view.MotionEvent;
import android.view.VelocityTracker;
import android.view.View;
import android.view.ViewConfiguration;
import android.view.ViewGroup;
import android.view.ViewParent;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.view.animation.Interpolator;
import com.android.ItemTouchHelper.R;
import com.android.ItemTouchHelper.util.Console;
import java.util.ArrayList;
import java.util.List;
//这个控件的拖拽还有BUG,有时交换元素时,元素会消失
//主要通过Demo来读源码流程,细节上不要太较真
@SuppressWarnings("all")
public class ItemTouchHelper extends RecyclerView.ItemDecoration implements RecyclerView.OnChildAttachStateChangeListener {
/**
* Up direction, used for swipe & drag control.
*/
public static final int UP = 1;
/**
* Down direction, used for swipe & drag control.
*/
public static final int DOWN = 1 << 1;
/**
* Left direction, used for swipe & drag control.
*/
public static final int LEFT = 1 << 2;
/**
* Right direction, used for swipe & drag control.
*/
public static final int RIGHT = 1 << 3;
// If you change these relative direction values, update Callback#convertToAbsoluteDirection,
// Callback#convertToRelativeDirection.
/**
* Horizontal start direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout
* direction. Used for swipe & drag control.
*/
public static final int START = LEFT << 2;
/**
* Horizontal end direction. Resolved to LEFT or RIGHT depending on RecyclerView's layout
* direction. Used for swipe & drag control.
*/
public static final int END = RIGHT << 2;
/**
* ItemTouchHelper is in idle state. At this state, either there is no related motion event by
* the user or latest motion events have not yet triggered a swipe or drag.
*/
public static final int ACTION_STATE_IDLE = 0;
/**
* A View is currently being swiped.
*/
public static final int ACTION_STATE_SWIPE = 1;
/**
* A View is currently being dragged.
*/
public static final int ACTION_STATE_DRAG = 2;
/**
* Animation type for views which are swiped successfully.
*/
public static final int ANIMATION_TYPE_SWIPE_SUCCESS = 1 << 1;
/**
* Animation type for views which are not completely swiped thus will animate back to their
* original position.
*/
public static final int ANIMATION_TYPE_SWIPE_CANCEL = 1 << 2;
/**
* Animation type for views that were dragged and now will animate to their final position.
*/
public static final int ANIMATION_TYPE_DRAG = 1 << 3;
static final int ACTIVE_POINTER_ID_NONE = -1;
static final int DIRECTION_FLAG_COUNT = 8;
private static final int ACTION_MODE_IDLE_MASK = (1 << DIRECTION_FLAG_COUNT) - 1;
static final int ACTION_MODE_SWIPE_MASK = ACTION_MODE_IDLE_MASK << DIRECTION_FLAG_COUNT;
static final int ACTION_MODE_DRAG_MASK = ACTION_MODE_SWIPE_MASK << DIRECTION_FLAG_COUNT;
/**
* The unit we are using to track velocity
*/
private static final int PIXELS_PER_SECOND = 1000;
/**
* Views, whose state should be cleared after they are detached from RecyclerView.
* This is necessary after swipe dismissing an item. We wait until animator finishes its job
* to clean these views.
*/
final List<View> mPendingCleanup = new ArrayList<View>();
/**
* Re-use array to calculate dx dy for a ViewHolder
*/
private final float[] mTmpPosition = new float[2];
/**
* Currently selected view holder
*/
ViewHolder mSelected = null;
ViewHolder mPreOpened = null;
/**
* The reference coordinates for the action start. For drag & drop, this is the time long
* press is completed vs for swipe, this is the initial touch point.
*/
float mInitialTouchX;
float mInitialTouchY;
/**
* Set when ItemTouchHelper is assigned to a RecyclerView.
*/
float mSwipeEscapeVelocity;
/**
* Set when ItemTouchHelper is assigned to a RecyclerView.
*/
float mMaxSwipeVelocity;
/**
* The diff between the last event and initial touch.
*/
float mDx;
float mDy;
/**
* The coordinates of the selected view at the time it is selected. We record these values
* when action starts so that we can consistently position it even if LayoutManager moves the
* View.
*/
float mSelectedStartX;
float mSelectedStartY;
/**
* The pointer we are tracking.
*/
int mActivePointerId = ACTIVE_POINTER_ID_NONE;
/**
* Developer callback which controls the behavior of ItemTouchHelper.
*/
Callback mCallback;
/**
* Current mode.
*/
int mActionState = ACTION_STATE_IDLE;
/**
* The direction flags obtained from unmasking
* {@link Callback#getAbsoluteMovementFlags(RecyclerView, ViewHolder)} for the current
* action state.
*/
int mSelectedFlags;
/**
* When a View is dragged or swiped and needs to go back to where it was, we create a Recover
* Animation and animate it to its location using this custom Animator, instead of using
* framework Animators.
* Using framework animators has the side effect of clashing with ItemAnimator, creating
* jumpy UIs.
*/
List<RecoverAnimation> mRecoverAnimations = new ArrayList<RecoverAnimation>();
private int mSlop;
RecyclerView mRecyclerView;
/**
* When user drags a view to the edge, we start scrolling the LayoutManager as long as View
* is partially out of bounds.
*/
//REMARK => 手指移到控件以外时,仍然继续拖拽
//这只是个细节功能,没有也不影响控件使用
final Runnable mScrollRunnable = new Runnable() {
@Override
public void run() {
// if (mSelected != null && scrollIfNecessary()) {
// if (mSelected != null) { //it might be lost during scrolling
// moveIfNecessary(mSelected);
// }
// mRecyclerView.removeCallbacks(mScrollRunnable);
// ViewCompat.postOnAnimation(mRecyclerView, this);
// }
}
};
/**
* Used for detecting fling swipe
*/
VelocityTracker mVelocityTracker;
//re-used list for selecting a swap target
private List<ViewHolder> mSwapTargets;
//re used for for sorting swap targets
private List<Integer> mDistances;
/**
* If drag & drop is supported, we use child drawing order to bring them to front.
*/
private RecyclerView.ChildDrawingOrderCallback mChildDrawingOrderCallback = null;
/**
* This keeps a reference to the child dragged by the user. Even after user stops dragging,
没有合适的资源?快使用搜索试试~ 我知道了~
资源详情
资源评论
资源推荐
收起资源包目录
demo-ui.zip (44个子文件)
demo-ui
gradlew.bat 2KB
gradlew 5KB
gradle.properties 89B
app
src
main
res
mipmap-xxxhdpi
ic_launcher.png 9KB
ic_launcher_round.png 15KB
drawable
ic_launcher_background.xml 6KB
mipmap-hdpi
ic_launcher.png 3KB
ic_launcher_round.png 5KB
drawable-v24
ic_launcher_foreground.xml 2KB
values
colors.xml 214B
strings.xml 81B
styles.xml 394B
dimens.xml 211B
mipmap-xxhdpi
ic_launcher.png 6KB
ic_launcher_round.png 10KB
mipmap-mdpi
ic_launcher.png 2KB
ic_launcher_round.png 3KB
mipmap-xhdpi
ic_launcher.png 4KB
ic_launcher_round.png 7KB
mipmap-anydpi-v26
ic_launcher_round.xml 276B
ic_launcher.xml 276B
layout
activity_main.xml 587B
list_item_with_single_delete.xml 1KB
list_item_main.xml 1KB
view_list_main_content.xml 1KB
java
com
android
ItemTouchHelper
Model.java 227B
view
MRecyclerView.java 2KB
MFrameLayout.java 1KB
HelperCallback.java 2KB
Adapter.java 7KB
MainActivity.java 2KB
util
Console.java 386B
MotionEvents.java 666B
helper
ItemTouchHelper.java 113KB
ItemTouchUIUtilImpl.java 882B
DividerItemDecoration.java 6KB
AndroidManifest.xml 696B
build.gradle 861B
gradle
wrapper
gradle-wrapper.jar 53KB
gradle-wrapper.properties 205B
RecyclerView侧滑源码解析.zip 603KB
local.properties 67B
settings.gradle 13B
build.gradle 1KB
共 44 条
- 1
命运之手
- 粉丝: 461
- 资源: 102
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论0