mam onLongPress i onDoubleTap działań umieszczonych na przycisk według tego kodu:Dlaczego android onLongPress zawsze jest uruchamiany po onDoubleTap?
...
GestureDetector detector = new GestureDetector(this, new TapDetector());
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, new TapDetector());
...
}
private class TapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public boolean onDoubleTap(MotionEvent e) {
// Do something
return true;
}
@Override
public void onLongPress(MotionEvent e) {
// Do something
}
}
Button incomeButton = (Button) findViewById(R.id.income);
button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
detector.onTouchEvent(event);
return true;
}
});
zawsze widzę onLongPress zwolniony po onDoubleClick zwolniony i stracony. Jaki jest powód takich sprzecznych z intuicją zachowań i jak tego uniknąć?
AKTUALIZACJA ja zmieniłem kod źródłowy, aby być bardziej szczegółowe
public class MainActivity extends Activity {
private GestureDetector detector;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
detector = new GestureDetector(this, new TapDetector());
Button button = (Button) findViewById(R.id.button);
button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("************* onTouch *************");
detector.onTouchEvent(event);
return true;
}
});
}
class TapDetector extends GestureDetector.SimpleOnGestureListener {
@Override
public void onLongPress(MotionEvent e) {
System.out.println("************* onLongPress *************");
}
@Override
public boolean onDoubleTap(MotionEvent e) {
System.out.println("************* onDoubleTap *************");
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NewActivity.class);
intent.putExtra("parameterName", "parameter");
startActivity(intent);
return true;
}
}
}
Jest to log po onDoubleTap kliknąłem. Możesz zobaczyć onLongPress na końcu - nigdy go nie klikam.
I/System.out(1106): ************* onTouch *************
I/System.out(1106): ************* onTouch *************
I/System.out(1106): ************* onTouch *************
I/System.out(1106): ************* onDoubleTap *************
I/ActivityManager( 59): Starting activity: Intent { cmp=my.tapdetector/.NewActivity (has extras) }
I/ActivityManager( 59): Displayed activity my.tapdetector/.NewActivity: 324 ms (total 324 ms)
I/System.out(1106): ************* onLongPress *************
UPDATE znalazłem rozwiązanie. Aby uniknąć onLongPress wypalania dwa zmiany należy zrobić:
pierwsze: detector.setIsLongpressEnabled (prawda); w onTouch (Zobacz przeciwko, wydarzenie MotionEvent)
button.setOnTouchListener(new OnTouchListener(){
@Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("************* onTouch *************");
detector.onTouchEvent(event);
detector.setIsLongpressEnabled(true);
return true;
}
});
drugie: dodać detector.setIsLongpressEnabled (fałszywy); w onDoubleTap (MotionEvent e)
public boolean onDoubleTap(MotionEvent e) {
detector.setIsLongpressEnabled(false);
System.out.println("************* onDoubleTap *************");
Intent intent = new Intent();
intent.setClass(getApplicationContext(), NewActivity.class);
intent.putExtra("parameterName", "parameter");
startActivity(intent);
return true;
}
Nie mogę tego powtórzyć, przepraszam. Używając Log.i() w tych 2 zdarzeniach, są one wywoływane poprawnie. –
Przyzwoita praca wokół tego, co wydaje się być wbudowanym warunkiem wyścigowym, jak opisano w komentarzach zaakceptowanej odpowiedzi. dzięki. –