正常滑动检测 Android Activity
Detection of swiping on a normal Android Activity
是否有可能在正常 Android activity 中检测到用户 left/right 的滑动?还是我必须使用 SwipeView?
根据https://developer.android.com/training/gestures/detector.html
您可以在 activity class 中实现 GestureDetector.OnGestureListener 并覆盖 onFling 或 onScroll
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
}
如果您正在寻找 Xamarin/C# 示例
在你的Activity
(或视图)上实现GestureDetector.IOnGestureListener
并在OnFling
方法中计算触摸方向:
[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
GestureDetector gestureDetector;
const int SWIPE_DISTANCE_THRESHOLD = 100;
const int SWIPE_VELOCITY_THRESHOLD = 100;
public bool OnDown(MotionEvent e)
{
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float distanceX = e2.GetX() - e1.GetX();
float distanceY = e2.GetY() - e1.GetY();
if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (distanceX > 0)
OnSwipeRight();
else
OnSwipeLeft();
return true;
}
return false;
}
void OnSwipeLeft()
{
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Text = "Swiped Left";
}
void OnSwipeRight()
{
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Text = "Swiped Right";
}
public void OnLongPress(MotionEvent e)
{
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e)
{
}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
public bool OnTouch(View v, MotionEvent e)
{
return gestureDetector.OnTouchEvent(e);
}
public override bool OnTouchEvent(MotionEvent e)
{
gestureDetector.OnTouchEvent(e);
return false;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
gestureDetector = new GestureDetector(this);
}
}
是否有可能在正常 Android activity 中检测到用户 left/right 的滑动?还是我必须使用 SwipeView?
根据https://developer.android.com/training/gestures/detector.html
您可以在 activity class 中实现 GestureDetector.OnGestureListener 并覆盖 onFling 或 onScroll
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
}
@Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
}
如果您正在寻找 Xamarin/C# 示例
在你的Activity
(或视图)上实现GestureDetector.IOnGestureListener
并在OnFling
方法中计算触摸方向:
[Activity(Label = "Swiper", MainLauncher = true, Icon = "@mipmap/icon")]
public class MainActivity : Activity, GestureDetector.IOnGestureListener
{
GestureDetector gestureDetector;
const int SWIPE_DISTANCE_THRESHOLD = 100;
const int SWIPE_VELOCITY_THRESHOLD = 100;
public bool OnDown(MotionEvent e)
{
return true;
}
public bool OnFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
float distanceX = e2.GetX() - e1.GetX();
float distanceY = e2.GetY() - e1.GetY();
if (Math.Abs(distanceX) > Math.Abs(distanceY) && Math.Abs(distanceX) > SWIPE_DISTANCE_THRESHOLD && Math.Abs(velocityX) > SWIPE_VELOCITY_THRESHOLD)
{
if (distanceX > 0)
OnSwipeRight();
else
OnSwipeLeft();
return true;
}
return false;
}
void OnSwipeLeft()
{
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Text = "Swiped Left";
}
void OnSwipeRight()
{
Button button = FindViewById<Button>(Resource.Id.myButton);
button.Text = "Swiped Right";
}
public void OnLongPress(MotionEvent e)
{
}
public bool OnScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
return false;
}
public void OnShowPress(MotionEvent e)
{
}
public bool OnSingleTapUp(MotionEvent e)
{
return false;
}
public bool OnTouch(View v, MotionEvent e)
{
return gestureDetector.OnTouchEvent(e);
}
public override bool OnTouchEvent(MotionEvent e)
{
gestureDetector.OnTouchEvent(e);
return false;
}
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
SetContentView(Resource.Layout.Main);
gestureDetector = new GestureDetector(this);
}
}