MotionEvent.ACTION_DOWN即使我返回true,也不会被调用

问题描述 投票:4回答:3

我的onTouchListenerLinearLayout有一个ListView,并且我试图使用ACTION_DOWNACTION_UP数据来检测用户何时滑至下一个ListView。但是,尽管MotionEvent可以完美运行,但ACTION_DOWN永远不等于ACTION_UP。经过大量的搜索之后,我唯一能找到的解决方案是在调用事件时返回true,但是我已经这样做了。这是我的onTouchListener代码

View.OnTouchListener mTouchListener = new View.OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                downX = event.getX();
                return true;
            } else if (event.getAction() == MotionEvent.ACTION_UP) {
                upX = event.getX();
                if(userSwipedFarEnough)
                    doStuff()
                return true;
            }
            return false;

        }

    };
android ontouchlistener motionevent
3个回答
3
投票

我知道发生了什么,我的列表视图的滚动视图以某种方式窃取了action_down,因此没有被调用。当我的列表视图为空并且滚动正常时,我才意识到这一点。


0
投票

根据触摸类型ACTION_DOWN ACTION_UP ACTION_MOVE,多次调用onTouch,所有这些可能一起发生。我想说出else if并仅使用if,它会捕获两个动作


0
投票

我的解决方案是扩展ScrollView:

interface MyScrollViewActionDownListener{
    fun onActionDown()
}

class MyScrollView: ScrollView
{
    private var mActionDownListener: MyScrollViewActionDownListener? = null
    constructor(context: Context, attributeSet: AttributeSet, defStyleAttr: Int): super(context, attributeSet, defStyleAttr)
    constructor(context: Context):super(context)
    constructor(context: Context, attributeSet: AttributeSet):super(context,attributeSet)

    override fun onInterceptTouchEvent(ev: MotionEvent?): Boolean {
        if(ev!!.action == MotionEvent.ACTION_DOWN){
            mActionDownListener?.onActionDown()
        }
        return super.onInterceptTouchEvent(ev)
    }

    fun setActionDownListener(listener: MyScrollViewActionDownListener){
        this.mActionDownListener = listener
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.