I want to access to the view who use a gestureListener inside a method in GestureListener
itself.
This is my listener:
public class GestureSwipeListener extends SimpleOnGestureListener {
final Context myContext;
// Costanti per lo swipe
private static final int SWIPE_THRESHOLD = 100;
private static final int SWIPE_VELOCITY_THRESHOLD = 100;
public GestureSwipeListener(Context context) {
myContext = context;
}
@Override
public boolean onDown(MotionEvent e) {
return super.onDown(e);
}
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
boolean result = false;
try {
float diffY = e2.getY() - e1.getY();
float diffX = e2.getX() - e1.getX();
if (Math.abs(diffX) > Math.abs(diffY)) {
if (Math.abs(diffX) > SWIPE_THRESHOLD
&& Math.abs(velocityX) > SWIPE_VELOCITY_THRESHOLD) {
if (diffX > 0) {
onSwipeRight();
} else {
onSwipeLeft();
}
}
} else {
if (Math.abs(diffY) > SWIPE_THRESHOLD
&& Math.abs(velocityY) > SWIPE_VELOCITY_THRESHOLD) {
if (diffY > 0) {
onSwipeBottom();
} else {
onSwipeTop();
}
}
}
} catch (Exception exception) {
exception.printStackTrace();
}
return result;
}
public boolean onSwipeRight() {
Log.d("FLING", "right");
return true;
}
public boolean onSwipeLeft() {
Log.d("FLING", "left");
return true;
}
public boolean onSwipeTop() {
Log.d("FLING", "top");
return true;
}
public boolean onSwipeBottom() {
Log.d("FLING", "bottom");
return true;
}
} // end class OnSwipeTouchListener
Then in my Activity I do this:
GestureSwipeListener gestureListener = new GestureSwipeListener(this);
GestireDetector gestureDetector = new GestureDetector(getApplicationContext(), gestureListener);
mainView = findViewById(R.id.main_view);
mainView.setClickable(true);
mainView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(gestureDetector.onTouchEvent(event)) {
return true;
}
return false;
}
});
Now I want to get the view resource (in this case mainView) directly inside the GestureSwipeListener
to execute some animations on that view.
I want to do this because I have to repeat the same animations on every single Activity in my application. So using the code directly in GestureListener
(in the onSwipeRight
, onSwipeLeft
methods) is better for avoiding the repetition of the same code again and again.
How can I do that?
Add parameters to your SwipeGestureListener constructor like:
In your gestureListener:
View myView;
public GestureSwipeListener(Context context, View view) {
myContext = context;
myView = view;
}
public doSomethingWithView() {
this.myView.setText("Foobar");
}
In your activity:
GestureSwipeListener gestureListener = new GestureSwipeListener(this.appContext, mainView);
...
Update 1:
View mainView = findViewById(R.id.main_view);
GestureSwipeListener gestureListener = new GestureSwipeListener(this.appContext, mainView);
Then dont forget in your GestureSwipeListener constructor:
View myView;
public GestureSwipeListener(Context context, View view) {
myContext = context;
myView = view;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With