Android gets view inside SimpleOnGestureListener

I want to access a view that uses gestureListener inside a method in a GestureListener . 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 perform some animations in this view.
I want to do this because I have to repeat the same animations for every action in the application. Therefore, using the code directly in the GestureListener (in the onSwipeRight , onSwipeLeft ), it is better to avoid repeating the same code again and again. How can i do this?

+4
source share
1 answer

Add parameters to your SwipeGestureListener constructor, for example:

In your gestures Listener:

 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 do not forget in your constructor GestureSwipeListener:

 View myView; public GestureSwipeListener(Context context, View view) { myContext = context; myView = view; } 
+5
source

All Articles