亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務條款》

Android TouchListener實現拖拽刪實例代碼

發布時間:2020-10-04 13:28:50 來源:腳本之家 閱讀:146 作者:lqh 欄目:移動開發

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

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

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

向AI問一下細節

免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。

AI

徐汇区| 永兴县| 探索| 旌德县| 瑞金市| 连平县| 商洛市| 日喀则市| 黄梅县| 无为县| 清徐县| 工布江达县| 聊城市| 葫芦岛市| 宣威市| 靖江市| 渭南市| 汾阳市| 固始县| 翁源县| 通州区| 玉环县| 丰顺县| 年辖:市辖区| 普格县| 咸阳市| 保定市| 大丰市| 双鸭山市| 桦川县| 开化县| 辛集市| 绥德县| 永靖县| 衡阳县| 牡丹江市| 日土县| 蒙山县| 新巴尔虎左旗| 溧水县| 河源市|