longPress 不回复 ACTION_UP - 简单的手势侦听器

longPress does not reply to ACTION_UP - simple gesture listener

我正在尝试使 imageView 在长按时变大,并在我松开后恢复正常。

public class MainActivity extends Activity {
private class Erjan_gestures extends SimpleOnGestureListener{
    @Override
    public void onLongPress(MotionEvent event) {
        Log.wtf("x", "long press occurring");

        switch (event.getAction()) {
            case MotionEvent.ACTION_DOWN:
                Log.wtf("x", "LONG PRESS - action down");
                image.getLayoutParams().height = 400;
                image.getLayoutParams().width = 400;
                RelativeLayout.LayoutParams for_answer1 = new   RelativeLayout.LayoutParams(300, 600);
                image.requestLayout();
                break ;
            case MotionEvent.ACTION_UP:
                //THIS CASE IS NEVER REACHED
                Log.wtf("x", "LONG PRESS - action up");
                image.getLayoutParams().height = oldH;
                image.getLayoutParams().width = oldW;
                image.requestLayout();
                break;
        }
    }
}

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    image= (ImageView)findViewById(R.id.card);
    button=(Button)findViewById(R.id.button);

    oldW = 500;
    oldH = 600;

    gestureDetector = new GestureDetector(new Erjan_gestures());
    gestureDetector.setIsLongpressEnabled(true);
    image.setOnTouchListener(new View.OnTouchListener(){

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if(event.getAction() == MotionEvent.ACTION_UP){
                Log.wtf("x", "action up is detected");
            }
            Log.wtf("x", "I m a card, and i know you click on me!");
            if(gestureDetector.onTouchEvent(event)) {
                Log.wtf("x", "this is onTouch(View v, MotionEvent event)");
                return true;
            }
            else return false;
        }
    });
}

但是我的 imageView 确实检测到长按并执行了 ACTION_DOWN,但从未进入 longpress()ACTION_UP 部分。

难道longpress不应该分成action_up,down吗?

  1. Long press gesture itself only consists of press(aka ACTION_DOWN)?
  2. why action_up in longPress never gets executed?

这确实是因为长按没有上下之分,而只有"trigger"一个动作。

实际上,ACTION_DOWN甚至是一个不正确的术语。 longpress 与 ACTION_DOWN 没有任何关系,因为 ACTION_DOWN 不会在用户按下按钮时立即触发。它只会在特定的按住延迟后被触发。因此,DELAY_PASSED 左右将是一个更合适的名称。

请注意,正常按下仍会继续,并且其 ACTION_UP 仍会触发。

这是使用Holder class 和onTouch() 方法提供的解决方案。

public class MainActivity extends AppCompatActivity {
    boolean is_pressed = false;    
    ImageView image;
    Button button;
    int oldW, oldH;

    private final Handler handler = new Handler();
    private final Runnable runnable = new Runnable() {
        public void run() {
            if (is_pressed) {
                Log.wtf("x", "LONG PRESS - action down");
                image.getLayoutParams().height = 400;
                image.getLayoutParams().width = 400;
                RelativeLayout.LayoutParams for_answer1 = new RelativeLayout.LayoutParams(300, 600);
                image.requestLayout();
            }
        }
    };

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);    
        image = (ImageView) findViewById(R.id.imageView);
        button = (Button) findViewById(R.id.button);    
        oldW = 500;
        oldH = 600;

        image.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
            Log.wtf("x", "touching");
            if (event.getAction() == MotionEvent.ACTION_DOWN) {
                is_pressed = true;
                // 500ms - to determine that this is a long press
                handler.postDelayed(runnable, 500);
            } else if (event.getAction() == MotionEvent.ACTION_UP && is_pressed) {                 
                Log.wtf("x", "LONG PRESS - action up");
                image.getLayoutParams().height = oldH;
                image.getLayoutParams().width = oldW;
                image.requestLayout();
                is_pressed = false;
            } else return false;
            return true;
        }
        });
    }
}