Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android TouchListener實現拖拽刪實例代碼

Android TouchListener實現拖拽刪實例代碼

編輯:關於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; 
  } 
} 

在使用時繼承該類, 並覆蓋三個回調方法, 就可以在合適的時機得到回調。

感謝閱讀,希望能幫助到大家,謝謝大家對本站的支持!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved