为activity增加左右手势识别(scrollview,gridview,listview适用)

来源:互联网 发布:查看iptables开放端口 编辑:程序博客网 时间:2024/05/21 10:35
/*
  *  左右手势
  *  1.复制下面的内容到目标Activity
  *  2.目标Activity的onCreate()调用initGesture()
  *  3.目标Activity需implements OnTouchListener, OnGestureListener
* 4.import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
  */
 privateGestureDetector mGestureDetector; privateint verticalMinDistance = 180; privateint minVelocity         = 0;  privatevoid initGesture() {     mGestureDetector = newGestureDetector((OnGestureListener) this); }  publicboolean onFling(MotionEvent e1, MotionEvent e2, floatvelocityX, floatvelocityY) {      if(e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {          // 切换Activity         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);         // startActivity(intent);         //Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();     }elseif (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {          // 切换Activity         // Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);         // startActivity(intent);         //Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();         finish(); //activty切换动画         overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);     }      returnfalse; }   @Override publicvoid onLongPress(MotionEvent arg0) {     // TODO Auto-generated method stub       }  @Override publicboolean onScroll(MotionEvent arg0, MotionEvent arg1, floatarg2,         floatarg3) {     // TODO Auto-generated method stub     returnfalse; }  @Override publicvoid onShowPress(MotionEvent arg0) {     // TODO Auto-generated method stub       }  @Override publicboolean onSingleTapUp(MotionEvent arg0) {     // TODO Auto-generated method stub     returnfalse; }  @Override publicboolean onTouch(View v, MotionEvent event) {     // TODO Auto-generated method stub // 触摸事件设置位mGestureDetector     returnmGestureDetector.onTouchEvent(event); }  @Override publicboolean onDown(MotionEvent arg0) {     // TODO Auto-generated method stub     returnfalse; }   @Override  publicboolean dispatchTouchEvent(MotionEvent ev) {// 把触摸事件分配给GestureDetector,重点一     mGestureDetector.onTouchEvent(ev);     returnsuper.dispatchTouchEvent(ev);}


0 0
原创粉丝点击