Determine what kind of VIEW in several views has a gesture (double-click, swipe left, swipe right, etc.)?

I have a scrollview with several horizontal relativelayout views ... one for each record in the database ... which are created programmatically.

I need to determine what kind of gestures affect the look ... click, double click and scroll left / right.

Of course, CLICK, which I could contact:

RelativeLayout rlView = new RelativeLayout(this); rlView.setId(10000+myrecordid); rlView.setOnClickListener(myviewclick); 

and myviewclick:

 private View.OnClickListener myviewclick = new View.OnClickListener() { public void onClick(View v) { Integer i=v.getId()-10000; // Do some processing on this view } }; 

From what I found on the Internet, I tried to make a gesture like this:

 rlView.setOnTouchListener(myviewtouch); 

using this code:

 private View.OnTouchListener myviewtouch = new View.OnTouchListener(){ public boolean onTouch(View v, MotionEvent event) { return gestureDetector.onTouchEvent(event); } GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDoubleTap(MotionEvent e) { Log.i("MYLOG","double tap"); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Log.i("MYLOG","SingleTapConfirmed"); return true; } @Override public void onLongPress(MotionEvent e) { Log.i("MYLOG","LongPress"); } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { if (e1.getX()<e2.getX()) Log.i("MYLOG","Fling Right"); else Log.i("MYLOG","Fling Left"); return true; } }); }; 

According to MYLOG, I get appropriate gestures as needed. The problem is that I do not know how to get the identifier of the view in which this gesture was. I know that it is in onTouch , but called gestureDetector.OnTouchEvent to determine the movement ... and I got lost at that point.

I searched all StackOverflow and other sites for several hours ... they all show deviations in how to define a gesture ... but without having problems finding any problems using several kinds that I can use.

Any help would be greatly appreciated.

+8
android view gesture
source share
1 answer

I think I get it. This is probably not the most elegant way to do this, and there may be other β€œproblems” due to its handling in this way, but at the moment it works. But if others find this problem, here is how I worked on it:

This is how my modified code worked. I added a public variable called vTouch :

 private View.OnTouchListener myviewtouch = new View.OnTouchListener(){ public View vTouch; public boolean onTouch(View v, MotionEvent event) { vTouch=v; return gestureDetector.onTouchEvent(event); } GestureDetector gestureDetector = new GestureDetector(new GestureDetector.SimpleOnGestureListener() { @Override public boolean onDoubleTap(MotionEvent e) { Integer viewId=vTouch.getId(); Log.i("MYLOG","double tap in view: "+viewId); return true; } @Override public boolean onSingleTapConfirmed(MotionEvent e) { Integer viewId=vTouch.getId(); Log.i("MYLOG","SingleTapConfirmed in view: "+viewId); return true; } @Override public void onLongPress(MotionEvent e) { Integer viewId=vTouch.getId(); Log.i("MYLOG","LongPress in view: "+viewId); } public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) { Integer viewId=vTouch.getId(); if (e1.getX()<e2.getX()) Log.i("MYLOG","Fling Right in view: "+viewId); else Log.i("MYLOG","Fling Left in view: "+viewId); return true; } }); }; 

Using the vTouch internal public variable, I can now refer to the view in every gesture event where the β€œview” is ever required for this gesture to appear.

+5
source share

All Articles