欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android TouchListener实现拖拽删实例代码

程序员文章站 2023-12-18 17:23:10
android touchlistener实现拖拽删实例代码 如果为一个控件设置了该触摸监听, 控件会随着用户的拖动而移动, 如果拖动的距离大过设置的临界值, 那么当松开...

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; 
  } 
} 

在使用时继承该类, 并覆盖三个回调方法, 就可以在合适的时机得到回调。

感谢阅读,希望能帮助到大家,谢谢大家对本站的支持!

上一篇:

下一篇: