简体   繁体   English

左右滑动时动作

[英]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.

相关问题 如何检测左/右和上/下之间的滑动方向 - How to detect swipe direction between left/right and up/down 根据速度或其他变量向左、向右、向上和向下滑动 - swipe left, right, up and down depending on velocity or other variables Android GestureDetector向上/向下滑动与向左/向右滑动 - Android GestureDetector Swipe Up/Down vs. Left/Right 全方位滑动手势(向左,向右,向上,向下) - Swipe gesture in all direction(Left,Right, Up, Down ) 通过向内和向外滑动(向上,向左,向右,向下)更改Android动画图片 - Android animation picture change with swipe in and out (up,left,right,down) Google眼镜:借助swipe_left和swipe_right上下滚动 - Google glass: Scroll up and down thanks to swipe_left and swipe_right 如何在基本四个方向(即右,左,上,下)上滑动视图中的图像 - how to swipe an image in a view in basic four directions i.e. right, left, up, down 在Android Wear活动中检测从左向右滑动的动作 - Detect left to right swipe action on Android wear activity 在GridView上向左滑动和向右滑动 - Swipe left and swipe right on GridView 科尔多瓦按上,下,左,右按钮 - Cordova catching UP, DOWN, LEFT, RIGHT buttons
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM