編輯:關於Android編程
Android TouchListener實現拖拽刪實例代碼
如果為一個控件設置了該觸摸監聽, 控件會隨著用戶的拖動而移動, 如果拖動的距離大過設置的臨界值, 那麼當松開手指時會有回調onDragComplete, 用戶可在該方法中將該控件從父布局中刪除, 或這進行其他操作。 如果用戶拖拽的距離小於臨界值, 那麼當用戶松開手指時控件會回談到原來的初始位置。這時會觸發onDragRebound回調。 如果用戶觸摸控件之後沒有拖拽而是直接松開手指, 會觸發onClick回調, 這樣用戶就不用為該控件設置onClick監聽。
源碼如下:
import android.util.Log; import android.view.MotionEvent; import android.view.View; import android.view.ViewGroup; /** * Created by zhangjg on 14-10-10. */ public class DragTouchListener implements View.OnTouchListener { /** * drag directions */ public static final int DIRECTION_UP = 0; public static final int DIRECTION_DOWN = 1; public static final int DIRECTION_LEFT = 2; public static final int DIRECTION_RIGHT = 3; private int mDragDirection = -1; private int mDragDistance = -1; private ViewGroup.MarginLayoutParams mParams; private ViewGroup.MarginLayoutParams mOriginParams; private int viewOriginMargin = -1000; private float mStartY = 0; private float mStartX = 0; private boolean isTouched = false; public DragTouchListener(int dragDirection, int dragDistance){ mDragDirection = dragDirection; mDragDistance = dragDistance; } protected void onClick(View view){ } protected void onDragComplete(View view){ } protected void onDragRebound(View view){ } @Override public boolean onTouch(View view, MotionEvent motionEvent) { if (viewOriginMargin == -1000){ mParams = (ViewGroup.MarginLayoutParams)view.getLayoutParams(); if (mDragDirection == DIRECTION_UP) { viewOriginMargin = mParams.bottomMargin; }else if (mDragDirection == DIRECTION_DOWN){ viewOriginMargin = mParams.topMargin; }else if (mDragDirection == DIRECTION_LEFT){ viewOriginMargin = mParams.rightMargin; }else if (mDragDirection == DIRECTION_RIGHT){ viewOriginMargin = mParams.leftMargin; } } int action = motionEvent.getAction(); switch (action){ case MotionEvent.ACTION_DOWN: isTouched = true; mStartY = motionEvent.getY(); mStartX = motionEvent.getX(); return true; case MotionEvent.ACTION_MOVE: float y = motionEvent.getY(); float x = motionEvent.getX(); if (mDragDirection == DIRECTION_UP){ if(y < mStartY){ mParams.bottomMargin = viewOriginMargin +(int) (mStartY - y); } }else if (mDragDirection == DIRECTION_DOWN){ if (y > mStartY){ mParams.topMargin = viewOriginMargin + (int) (y - mStartY); } }else if (mDragDirection == DIRECTION_LEFT){ if (x < mStartX){ mParams.rightMargin = viewOriginMargin + (int) (mStartX - x); } }else if (mDragDirection == DIRECTION_RIGHT){ if (x > mStartX){ mParams.leftMargin = viewOriginMargin + (int) (x - mStartX); } } view.setLayoutParams(mParams); break; case MotionEvent.ACTION_UP: float nowY = motionEvent.getY(); float nowX = motionEvent.getX(); int deltaX = (int)nowX - (int)mStartX; int deltaY = (int)nowY - (int)mStartY; if (isTouched && Math.abs(deltaX) < 5 && Math.abs(deltaY) < 5){ onClick(view); break; } if (mDragDirection == DIRECTION_UP){ if (isTouched && mStartY - nowY > mDragDistance){ // Log.i("test-drag", "direction up , startY = " + mStartY + ", nowY = " + nowY + // ", startY - nowY = " + (mStartY - nowY) + ", dragDistance : " + mDragDistance); onDragComplete(view); }else if (mStartY - nowY > 0 && mStartY - nowY < mDragDistance ){ mParams.bottomMargin = viewOriginMargin; view.setLayoutParams(mParams); onDragRebound(view); } }else if (mDragDirection == DIRECTION_DOWN){ if (isTouched && nowY - mStartY > mDragDistance){ onDragComplete(view); }else if ( nowY - mStartY > 0 && nowY - mStartY < mDragDistance ){ mParams.topMargin = viewOriginMargin; view.setLayoutParams(mParams); onDragRebound(view); } }else if (mDragDirection == DIRECTION_LEFT){ if (isTouched && mStartX - nowX > mDragDistance){ onDragComplete(view); }else if ( mStartX - nowX > 0 && mStartX - nowX < mDragDistance ){ mParams.rightMargin = viewOriginMargin; view.setLayoutParams(mParams); onDragRebound(view); } }else if (mDragDirection == DIRECTION_RIGHT){ if (isTouched && nowX - mStartX > mDragDistance){ onDragComplete(view); }else if ( nowX - mStartX > 0 && nowX - mStartX < mDragDistance ){ mParams.leftMargin = viewOriginMargin; view.setLayoutParams(mParams); onDragRebound(view); } } isTouched = false; break; } return false; } }
在使用時繼承該類, 並覆蓋三個回調方法, 就可以在合適的時機得到回調。
感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!
本文實例講述了Android編程中TextView寬度過大導致Drawable無法居中問題解決方法。分享給大家供大家參考,具體如下:在做項目的時候,很多時候我們都要用到文
先看看效果圖主要思想:1、監聽觸碰事件2、用WindowManager添加拖曳的圖片3、用Collections.swap()交換List數據自定義代碼:public c
在Xamarin android如何調用百度地圖呢?首先我們要區分清楚,百度地圖這是一個廣泛的概念,很多剛剛接觸這個名詞”百度地圖api”,的確是
在Android中和我們打交道最多的就是Activity,因為我們會頻繁的與界面進行交互,而Activity內部關於界面方面的操作都是由Window來實現的,因此我們有必