仿微信朋友圈双击顶部回到最前端(GestureDetector.OnDoubleTapListener)
listView.setSelection(0);
publicclassMainActivityextendsActivityimplementsOnTouchListener{
privateGestureDetectormGestureDetector;
@Override
protectedvoidonCreate(BundlesavedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGestureDetector=newGestureDetector(newgestureListener());//使用派生自OnGestureListener
mGestureDetector.setOnDoubleTapListener(newdoubleTapListener());
TextViewtv=(TextView)findViewById(R.id.tv);
tv.setOnTouchListener(this);
tv.setFocusable(true);
tv.setClickable(true);
tv.setLongClickable(true);
}
/*
*在onTouch()方法中,我们调用GestureDetector的onTouchEvent()方法,将捕捉到的MotionEvent交给GestureDetector
*来分析是否有合适的callback函数来处理用户的手势
*/
publicbooleanonTouch(Viewv,MotionEventevent){
returnmGestureDetector.onTouchEvent(event);
}
//OnGestureListener监听
privateclassgestureListenerimplementsGestureDetector.OnGestureListener{
publicbooleanonDown(MotionEvente){
Log.i("MyGesture","onDown");
Toast.makeText(MainActivity.this,"onDown",Toast.LENGTH_SHORT).show();
returnfalse;
}
publicvoidonShowPress(MotionEvente){
Log.i("MyGesture","onShowPress");
Toast.makeText(MainActivity.this,"onShowPress",Toast.LENGTH_SHORT).show();
}
publicbooleanonSingleTapUp(MotionEvente){
Log.i("MyGesture","onSingleTapUp");
Toast.makeText(MainActivity.this,"onSingleTapUp",Toast.LENGTH_SHORT).show();
returntrue;
}
publicbooleanonScroll(MotionEvente1,MotionEvente2,
floatdistanceX,floatdistanceY){
Log.i("MyGesture22","onScroll:"+(e2.getX()-e1.getX())+""+distanceX);
Toast.makeText(MainActivity.this,"onScroll",Toast.LENGTH_LONG).show();
returntrue;
}
publicvoidonLongPress(MotionEvente){
Log.i("MyGesture","onLongPress");
Toast.makeText(MainActivity.this,"onLongPress",Toast.LENGTH_LONG).show();
}
publicbooleanonFling(MotionEvente1,MotionEvente2,floatvelocityX,
floatvelocityY){
Log.i("MyGesture","onFling");
Toast.makeText(MainActivity.this,"onFling",Toast.LENGTH_LONG).show();
returntrue;
}
};
//OnDoubleTapListener监听
privateclassdoubleTapListenerimplementsGestureDetector.OnDoubleTapListener{
publicbooleanonSingleTapConfirmed(MotionEvente){
Log.i("MyGesture","onSingleTapConfirmed");
Toast.makeText(MainActivity.this,"onSingleTapConfirmed",Toast.LENGTH_LONG).show();
returntrue;
}
publicbooleanonDoubleTap(MotionEvente){
Log.i("MyGesture","onDoubleTap");
Toast.makeText(MainActivity.this,"onDoubleTap",Toast.LENGTH_LONG).show();
returntrue;
}
publicbooleanonDoubleTapEvent(MotionEvente){
Log.i("MyGesture","onDoubleTapEvent");
Toast.makeText(MainActivity.this,"onDoubleTapEvent",Toast.LENGTH_LONG).show();
returntrue;
}
};
}
参考:
用户手势检测-GestureDetector使用详解声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。