[英]On swipe left, right, up, down action
I have trouble with coding a swipe listener that would start a function. 我在编写可以启动功能的滑动监听器时遇到麻烦。 But I don't want to use fragments.
但是我不想使用片段。
Here's the code: 这是代码:
class Dock : AppCompatActivity(), GestureDetector.OnGestureListener
{
override fun onCreate(savedInstanceState: Bundle?)
{
super.onCreate(savedInstanceState)
setContentView(R.layout.dock)
hide_bars()
}
private val swipeThreshold = 100
private val swipeVelocityThreshold = 100
override fun onFling(downEvent: MotionEvent, moveEvent: MotionEvent, velocityX: Float, velocityY: Float): Boolean
{
var result = false
val diffY = moveEvent.y - downEvent.y
val diffX = moveEvent.x - downEvent.x
if (Math.abs(diffX) > Math.abs(diffY))
{
// right or left swipe
if (Math.abs(diffX) > swipeThreshold && Math.abs(velocityX) > swipeVelocityThreshold)
{
if (diffX > 0)
{
swipeRight()
} else
{
swipeLeft()
}
result = true
}
} else
{
// up or down swipe
if (Math.abs(diffY) > swipeThreshold && Math.abs(velocityY) > swipeVelocityThreshold)
{
if (diffY > 0)
{
swipeDown()
} else
{
swipeUp()
}
result = true
}
}
return result
}
private fun swipeUp()
{
Toast.makeText(this, "Swipe Top", Toast.LENGTH_LONG).show()
}
private fun swipeDown()
{
Toast.makeText(this, "Swipe Bottom", Toast.LENGTH_LONG).show()
}
private fun swipeLeft()
{
Toast.makeText(this, "Swipe Left", Toast.LENGTH_LONG).show()
}
private fun swipeRight()
{
Toast.makeText(this, "Swipe Right", Toast.LENGTH_LONG).show()
}
I can start the app with no problem, however, when I swipe, nothing changes. 我可以毫无问题地启动应用程序,但是,当我滑动时,没有任何变化。 No toasts, no activity started that would change the layout.
没有敬酒,没有任何活动会改变布局。 That's why I'm here.
这就是为什么我在这里。 Please help me.
请帮我。
"After swipe" functions are not comprised in the code, because they are not the problem (tested) and this post couldn't be created. 代码中不包含“刷卡后”功能,因为这不是问题(已测试),因此无法创建此帖子。
Activities don't receive touch input. 活动不接收触摸输入。 Views do.
意见呢。 You'd need to set the onTouchListener of some view within your app to implement this behavior.
您需要在应用程序中设置某些视图的onTouchListener才能实现此行为。 In addition, since your app will probably have subviews you can't just put it on the root view alone- you'll have to implement onInterceptTouchEvent as well to steal touch events from the child views when appropriate (please note doing this poorly may break some behavior of child views).
此外,由于您的应用程序可能会有子视图,因此您不能仅将其放在根视图上-您还必须实现onInterceptTouchEvent并在适当时从子视图中窃取触摸事件(请注意,这样做可能会中断子视图的某些行为)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.