Reputation: 179
I want to use Gesture Listener so I wrote a simple code to test it out but it is not working. I watched a YouTube tutorial and copied it. I did not test this but it is something very similar to what I am trying to accomplish. This is my first time using Gesture. Here is what I am:
MyClassActivity.java
public class MyClassActivity extends Activity implments GestureDetector.OnGestureListener, GestureDetector.OnDoubleTapListener{
private GestureDetector gestureDetector;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scrolling);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
this.gestureDetector = new GestureDetector(this, this);
gestureDetector.setOnDoubleTapListener(this);
textView = (TextView) findViewById(R.id.textView);
} //end of Oncreate
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return super.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent e) {
textView.setText("onDown");
return false;
}
@Override
public void onShowPress(MotionEvent e) {
textView.setText("onShowPress");
}
@Override
public boolean onSingleTapUp(MotionEvent e) {
textView.setText("onSingleTap");
return false;
}
@Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
textView.setText("onScroll");
return false;
}
@Override
public void onLongPress(MotionEvent e) {
textView.setText("onLongPress");
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
return false;
}
@Override
public boolean onSingleTapConfirmed(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTap(MotionEvent e) {
return false;
}
@Override
public boolean onDoubleTapEvent(MotionEvent e) {
return false;
}
Upvotes: 0
Views: 52
Reputation: 364
Try this at the end of your onCreate method.
View v = //Get your view
v.setOnTouchListener(new OnTouchListener(){
public boolean onTouch(View v, MotionEvent e){
return gestureDetector.onTouchEvent(e);
}
});
Upvotes: 2
Reputation: 3275
Can you try with
@Override
public boolean onTouchEvent(MotionEvent event) {
this.gestureDetector.onTouchEvent(event);
return true; // paas it true , as you are handling it.
}
Upvotes: 1