gpt4 book ai didi

android - 在正常的 Android Activity 上检测滑动

转载 作者:太空宇宙 更新时间:2023-11-03 12:47:56 25 4
gpt4 key购买 nike

是否有可能在正常的 Android Activity 中检测到用户向左/向右滑动?还是我必须使用 SwipeView?

最佳答案

如果您正在寻找 Xamarin/C# 示例

在您的Activity(或 View )上实现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 - 在正常的 Android Activity 上检测滑动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38722272/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com