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

时间:2015-09-22 07:05:29

标签: android gesturedetector gesturelistener

我试图让imageView在长按时增加尺寸,并在我压下它后恢复正常。

var a = registration as ComponentRegistration<A>;

但是我的imageView会检测到longpress并执行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; } }); } ,但永远不会到ACTION_DOWN ACTION_UP

这是因为longpress不应分为action_up,down?

  
      
  1. 长按手势本身只包括按(又名ACTION_DOWN)?
  2.   
  3. 为什么longPress中的action_up永远不会被执行?
  4.   

2 个答案:

答案 0 :(得分:1)

这确实是因为longpress没有分为向下和向上,而是只有一个&#34;触发器&#34;行动。

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

请注意,正常按下仍然继续,并且其ACTION_UP仍然会闪光。

答案 1 :(得分:1)

以下是使用Holder类和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;
        }
        });
    }
}