OnClickListener在Android中被调用两次

时间:2016-07-26 10:35:29

标签: android onclicklistener onlongclicklistener

我有以下用例:当点击View时,它应该会打开Activity,并且在长时间点击后,它应该在最短的时间后打开另一个Activity ,300毫秒或用户的ACTION_UP事件。 我为此编写了以下代码:

holder.fiveAction.setOnLongClickListener(new View.OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                //open activity A after launching an animation of 300ms
                isLongPressed = true;
                return true;
            }
        });
        holder.fiveAction.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                // We're only interested in when the button is released.
                if (event.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    if (isLongPressed) {
                        //open activity A even if animation hasn't ended
                        // Do something when the button is released.
                        isLongPressed = false;
                    }
                }
                return false;
            }
        });

        holder.fiveAction.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                LogUtil.i(TAG, "hi five onClickListener called");
                //open some activity B or do some work
        });

此设置的问题是OnClickListener被调用两次,我不想要。我想知道为什么会发生这种情况,如何在不影响我的用例的情况下解决这个问题。

2 个答案:

答案 0 :(得分:2)

您使用三个Listener 1。OnLongClickListener 2。OnTouchListener 3。OnClickListener

单击View First时,它会调用OnTouchListener,然后调用OnClickListener 这就是为什么你的事件会引发两次......

使用此技巧我相信你的问题会解决......我自己测试

    holder.fiveAction.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            //open activity A after launching an animation of 300ms

            holder.fiveAction.setOnTouchListener(new View.OnTouchListener() {
            @Override
               public boolean onTouch(View v, MotionEvent event) {
                v.onTouchEvent(event);
                 // We're only interested in when the button is released.
                 if (event.getAction() == MotionEvent.ACTION_UP) {
                    // We're only interested in anything if our speak button is currently pressed.
                    //open activity A even if animation hasn't ended
                    // Do something when the button is released.

                 }
              return false;
             }
            });
            return true;
        }
    });

    holder.fiveAction.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            LogUtil.i(TAG, "hi five onClickListener called");
            //open some activity B or do some work
    });

您需要将holder.fiveAction声明为final

答案 1 :(得分:0)

我遇到这个问题是因为 Android Debugger 有问题,正常运行时该函数被调用一次,连接调试器后它被调用两次。