下面列出了怎么用android.support.v4.view.MotionEventCompat的API类实例代码及写法,或者点击链接到github查看源代码。
@Override
public boolean onTouchEvent(MotionEvent ev) {
if (!mCanSlide) return super.onTouchEvent(ev);
mDragHelper.processTouchEvent(ev);
final int action = ev.getAction();
boolean wantTouchEvents = true;
switch (action & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_DOWN: {
final float x = ev.getX();
final float y = ev.getY();
mInitialMotionX = x;
mInitialMotionY = y;
break;
}
case MotionEvent.ACTION_UP: {
break;
}
}
return wantTouchEvents;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = MotionEventCompat.getActionMasked(ev);
if (stopWhenTouch) {
switch (action) {
case MotionEvent.ACTION_DOWN:
if (isAutoScroll) {
isStopedWhenTouch = true;
stopAutoScroll();
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_OUTSIDE:
if (isStopedWhenTouch) {
startAutoScroll();
}
break;
}
}
return super.dispatchTouchEvent(ev);
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
int action = MotionEventCompat.getActionMasked(ev);
if (action == MotionEvent.ACTION_DOWN)
{
stopScroll();
}
else if (ev.getAction() == MotionEvent.ACTION_UP)
{
resumeScroll();
}
this.getParent().requestDisallowInterceptTouchEvent(true);
return super.dispatchTouchEvent(ev);
}
private void determineDrag(MotionEvent ev) {
final int activePointerId = mActivePointerId;
final int pointerIndex = getPointerIndex(ev, activePointerId);
if (activePointerId == INVALID_POINTER || pointerIndex == INVALID_POINTER)
return;
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float dy = y - mLastMotionY;
final float yDiff = Math.abs(dy);
if (xDiff > (isMenuOpen() ? mTouchSlop / 2 : mTouchSlop) && xDiff > yDiff && thisSlideAllowed(dx)) {
startDrag();
mLastMotionX = x;
mLastMotionY = y;
setScrollingCacheEnabled(true);
// TODO add back in touch slop check
} else if (xDiff > mTouchSlop) {
mIsUnableToDrag = true;
}
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
switch (MotionEventCompat.getActionMasked(ev)) {
case MotionEvent.ACTION_DOWN:
case MotionEvent.ACTION_MOVE:
case MotionEventCompat.ACTION_POINTER_DOWN:
mHandler.removeMessages(AUTO_SCROLL_MESSAGE);
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
case MotionEvent.ACTION_POINTER_2_UP:
case MotionEvent.ACTION_POINTER_3_UP:
case MotionEvent.ACTION_CANCEL:
mHandler.removeMessages(AUTO_SCROLL_MESSAGE);
mHandler.sendEmptyMessageDelayed(AUTO_SCROLL_MESSAGE, delayTimeInMills);
break;
default:
break;
}
return super.onTouchEvent(ev);
}
private void onSecondaryPointerUp(MotionEvent ev) {
final int pointerIndex = (ev.getAction() & MotionEventCompat.ACTION_POINTER_INDEX_MASK) >>
MotionEventCompat.ACTION_POINTER_INDEX_SHIFT;
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
// TODO: Make this decision more intelligent.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastMotionY = (int) MotionEventCompat.getY(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
if (mVelocityTracker != null) {
mVelocityTracker.clear();
}
}
}
private void determineDrag(MotionEvent ev) {
final int activePointerId = mActivePointerId;
final int pointerIndex = getPointerIndex(ev, activePointerId);
if (activePointerId == INVALID_POINTER || pointerIndex == INVALID_POINTER)
return;
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float dx = x - mLastMotionX;
final float xDiff = Math.abs(dx);
final float y = MotionEventCompat.getY(ev, pointerIndex);
final float dy = y - mLastMotionY;
final float yDiff = Math.abs(dy);
if (xDiff > (isMenuOpen()?mTouchSlop/2:mTouchSlop) && xDiff > yDiff && thisSlideAllowed(dx)) {
startDrag();
mLastMotionX = x;
mLastMotionY = y;
setScrollingCacheEnabled(true);
// TODO add back in touch slop check
} else if (xDiff > mTouchSlop) {
mIsUnableToDrag = true;
}
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
int action = MotionEventCompat.getActionMasked(ev);
if (stopScrollWhenTouch) {
if ((action == MotionEvent.ACTION_DOWN)) {
if (mOnTouchListening!=null){
mOnTouchListening.onTouchCancleTimer(true);
}
} else if (ev.getAction() == MotionEvent.ACTION_UP ) {
if (mOnTouchListening!=null){
mOnTouchListening.onTouchCancleTimer(false);
}
}
}
return super.dispatchTouchEvent(ev);
}
private ViewHolder findSwipedView(MotionEvent motionEvent) {
LayoutManager lm = this.mRecyclerView.getLayoutManager();
if (this.mActivePointerId == -1) {
return null;
}
int pointerIndex = MotionEventCompat.findPointerIndex(motionEvent, this.mActivePointerId);
float dy = MotionEventCompat.getY(motionEvent, pointerIndex) - this.mInitialTouchY;
float absDx = Math.abs(MotionEventCompat.getX(motionEvent, pointerIndex) - this.mInitialTouchX);
float absDy = Math.abs(dy);
if (absDx < ((float) this.mSlop) && absDy < ((float) this.mSlop)) {
return null;
}
if (absDx > absDy && lm.canScrollHorizontally()) {
return null;
}
if (absDy > absDx && lm.canScrollVertically()) {
return null;
}
View child = findChildView(motionEvent);
if (child != null) {
return this.mRecyclerView.getChildViewHolder(child);
}
return null;
}
@Override
public boolean onTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
if (mReturningToStart && action == MotionEvent.ACTION_DOWN) {
mReturningToStart = false;
}
if (!isEnabled() || mReturningToStart
|| (!isChildScrollToTop() && !isChildScrollToBottom())) {
// 如果子View可以滑动,不拦截事件,交给子View处理
return false;
}
if (isChildScrollToBottom()) {// 上拉加载更多
return handlerPushTouchEvent(ev, action);
} else {// 下拉刷新
return handlerPullTouchEvent(ev, action);
}
}
@Override
public void onTouchEvent(RecyclerView rv, MotionEvent e)
{
if (!mIsActive)
return;
int action = MotionEventCompat.getActionMasked(e);
switch (action)
{
case MotionEvent.ACTION_MOVE:
if (!mInTopSpot && !mInBottomSpot)
updateSelectedRange(rv, e);
processAutoScroll(e);
break;
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_POINTER_UP:
reset();
break;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (MotionEventCompat.getActionMasked(event)){
case MotionEvent.ACTION_DOWN:
if (!isWindowFocused){
return true;
}
if (animationState == STATE_EXPANDED){
startWholeAnimation();
}
this.callOnClick();
return true;
case MotionEvent.ACTION_UP:
return false;
}
return false;
}
@Override
public boolean onTouchEvent( CoordinatorLayout parent, V child, MotionEvent event ) {
if ( ! child.isShown() ) {
return false;
}
int action = MotionEventCompat.getActionMasked( event );
if ( mState == STATE_DRAGGING && action == MotionEvent.ACTION_DOWN ) {
toggleHeaderColor(true);
return true;
}
if (mViewDragHelper == null) {
mViewDragHelper = ViewDragHelper.create(parent, mDragCallback);
}
mViewDragHelper.processTouchEvent( event );
if ( action == MotionEvent.ACTION_DOWN ) {
reset();
}
// The ViewDragHelper tries to capture only the top-most View. We have to explicitly tell it
// to capture the bottom sheet in case it is not captured and the touch slop is passed.
if ( action == MotionEvent.ACTION_MOVE && ! mIgnoreEvents ) {
if ( Math.abs(mInitialY - event.getY()) > mViewDragHelper.getTouchSlop() ) {
mViewDragHelper.captureChildView( child, event.getPointerId(event.getActionIndex()) );
}
}
return ! mIgnoreEvents;
}
private void saveLastMotion(MotionEvent ev) {
final int pointerCount = MotionEventCompat.getPointerCount(ev);
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
mLastMotionX[pointerId] = x;
mLastMotionY[pointerId] = y;
}
}
@Override
public void onBindViewHolder(final CategoryViewHolder holder, int position) {
if (position >= getItems().size()) {
holder.itemView.setVisibility(View.INVISIBLE);
return;
} else {
holder.itemView.setVisibility(View.VISIBLE);
}
Category category = get(position);
holder.name.setText(category.getName());
holder.color.setBackgroundColor(category.getPrimaryColor());
holder.show.setChecked(category.isVisible());
if (mRssPath == null) {
holder.edit.setOnClickListener(new CategoryItemClickListener(category));
holder.color.setOnClickListener(new CategoryItemClickListener(category));
holder.show.setOnClickListener(new CategoryItemClickListener(category));
holder.more.setOnClickListener(new CategoryItemClickListener(category));
holder.drag.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
if (MotionEventCompat.getActionMasked(event) == MotionEvent.ACTION_DOWN) {
mItemTouchHelper.startDrag(holder);
}
return false;
}
});
} else {
holder.edit.setVisibility(View.GONE);
holder.drag.setVisibility(View.GONE);
holder.show.setVisibility(View.GONE);
holder.more.setVisibility(View.GONE);
}
holder.itemView.setOnLongClickListener(new CategoryItemLongClickListener(category));
holder.itemView.setOnClickListener(new CategoryItemRSSClickListener(category));
setBackground(holder.itemView);
}
private float getMotionEventY(MotionEvent ev, int activePointerId) {
final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
if (index < 0) {
return -1;
}
return MotionEventCompat.getY(ev, index);
}
private void saveLastMotion(MotionEvent ev) {
final int pointerCount = MotionEventCompat.getPointerCount(ev);
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
mLastMotionX[pointerId] = x;
mLastMotionY[pointerId] = y;
}
}
@Override
public boolean onTouch(View v, MotionEvent event) {
if(MotionEvent.ACTION_DOWN == MotionEventCompat.getActionMasked(event)){
startDetectingDrag(view);
}
return false;
}
private void sendCancelEventToView(View view, MotionEvent motionEvent) {
((ViewGroup) view).requestDisallowInterceptTouchEvent(true);
MotionEvent cancelEvent = MotionEvent.obtain(motionEvent);
cancelEvent.setAction(MotionEvent.ACTION_CANCEL |
(MotionEventCompat.getActionIndex(motionEvent) << MotionEventCompat.ACTION_POINTER_INDEX_SHIFT));
view.onTouchEvent(cancelEvent);
}
private float getMotionEventY(MotionEvent ev, int activePointerId) {
final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
if (index < 0) {
return -1;
}
return MotionEventCompat.getY(ev, index);
}
private void onSecondaryPointerUp(MotionEvent ev) {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
}
/**
* Override method to intercept only touch events over the drag view and to cancel the drag when
* the action associated to the MotionEvent is equals to ACTION_CANCEL or ACTION_UP.
*
* @param ev captured.
* @return true if the view is going to process the touch event or false if not.
*/
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
if (!isEnabled()) {
return false;
}
mDraggedView = null;
stream(mViews)
.filter(v -> isViewHit(v, (int) ev.getX(), (int) ev.getY()))
.forEach((Action1<BorderView>) v -> mDraggedView = v);
final int actionMasked = MotionEventCompat.getActionMasked(ev);
switch (actionMasked & MotionEventCompat.ACTION_MASK) {
case MotionEvent.ACTION_CANCEL:
case MotionEvent.ACTION_UP:
hideDraggedViewBorder();
mViewDragHelper.cancel();
return false;
case MotionEvent.ACTION_DOWN:
showDraggedViewBorder();
mViewDragHelper.processTouchEvent(ev);
return false;
default:
break;
}
return mViewDragHelper.shouldInterceptTouchEvent(ev) ||
mViewDragHelper.isViewUnder(mDraggedView, (int) ev.getX(), (int) ev.getY());
}
private void onSecondaryPointerUp(MotionEvent ev) {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
}
}
@Override
public boolean onTouchEvent(MotionEvent event) {
final boolean handled = super.onTouchEvent(event);
final int action = event.getAction() & MotionEventCompat.ACTION_MASK;
if (action == MotionEvent.ACTION_UP) {
mGestureListener.dispatchSingleTapUpIfNeeded(event);
}
return handled;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
final int action = MotionEventCompat.getActionMasked(ev);
switch (action) {
case MotionEvent.ACTION_DOWN: {
try {
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final float x = MotionEventCompat.getX(ev, pointerIndex);
final float y = MotionEventCompat.getY(ev, pointerIndex);
// Remember where we started (for dragging)
lastTouchX = x;
lastTouchY = y;
// Save the ID of this pointer (for dragging)
activePointerId = MotionEventCompat.getPointerId(ev, 0);
} catch (IllegalArgumentException ex) { // pointerIndex out of range
Log.e(TAG, "IllegalArgumentException: " + ex.getMessage());
return false;
}
break;
}
case MotionEvent.ACTION_UP: {
onMouseActionUp(ev);
break;
}
case MotionEvent.ACTION_CANCEL: {
cancelDrag();
break;
}
}
return isDragging;
}
float getMotionEventY(MotionEvent ev, int activePointerId) {
int index = MotionEventCompat.findPointerIndex(ev,
activePointerId);
if (index < 0) {
return -1;
}
return MotionEventCompat.getY(ev, index);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
if (!isEnabled()) {
return false;
}
int actionMasked = MotionEventCompat.getActionMasked(event);
switch (actionMasked) {
case MotionEvent.ACTION_DOWN:
mDownX = event.getX();
startDragging(event, isInScrollingContainer());
break;
case MotionEvent.ACTION_MOVE:
if (isDragging()) {
updateDragging(event);
} else {
final float x = event.getX();
if (Math.abs(x - mDownX) > mTouchSlop) {
startDragging(event, false);
}
}
break;
case MotionEvent.ACTION_UP:
case MotionEvent.ACTION_CANCEL:
stopDragging();
break;
}
return true;
}
private void saveLastMotion(MotionEvent ev) {
final int pointerCount = MotionEventCompat.getPointerCount(ev);
for (int i = 0; i < pointerCount; i++) {
final int pointerId = MotionEventCompat.getPointerId(ev, i);
final float x = MotionEventCompat.getX(ev, i);
final float y = MotionEventCompat.getY(ev, i);
mLastMotionX[pointerId] = x;
mLastMotionY[pointerId] = y;
}
}
private void onSecondaryPointerUp(MotionEvent ev) {
if (DEBUG) Log.v(TAG, "onSecondaryPointerUp called");
final int pointerIndex = MotionEventCompat.getActionIndex(ev);
final int pointerId = MotionEventCompat.getPointerId(ev, pointerIndex);
if (pointerId == mActivePointerId) {
// This was our active pointer going up. Choose a new
// active pointer and adjust accordingly.
final int newPointerIndex = pointerIndex == 0 ? 1 : 0;
mLastMotionX = MotionEventCompat.getX(ev, newPointerIndex);
mActivePointerId = MotionEventCompat.getPointerId(ev, newPointerIndex);
if (mVelocityTracker != null) {
mVelocityTracker.clear();
}
}
}
private float getMotionEventY(MotionEvent ev, int activePointerId) {
final int index = MotionEventCompat.findPointerIndex(ev, activePointerId);
if (index < 0) {
return -1;
}
return MotionEventCompat.getY(ev, index);
}