Fling and scroll movements - java

My View implements onGestureListener to let me be informed when a gesture is done by the user, either if it is a scroll or a fling movement.
When the pointer is moved slowly on the screen, I receive onScroll events as expected.But when the pointer is moved faster, I get several onScroll events before getting the onFling event. As the onFling event passes the event associated with the "up" event, I suppose it is only triggered after the user takes his finger away from the screen.
Ans this causes a not natural behaviour for my application...
My question is : is it possible to suppress all the preceding onScroll events when the move executed by the user is clearly a fling movement?

Problem solved, but the solution doesn't look very clean : in the "onFling" function, I cancel every treatments realized in the "onScroll" function and I only take the "onFling" event into account if one of the velocities is greater than 1000 in absolute value.

Related

Gesture Detection for Specific View

Added a spaceView that doesn't cover all of the layout. And I want gesture detection work for only that view.
Couldn't do much but here is my codes:
space = findViewById(R.id.space);
this.gDetector = new GestureDetectorCompat(this, this);
gDetector.setOnDoubleTapListener(this);
The onTouchEvent in Android lets you do so. You can add whatever you want to do in that event.
As per Android Studio's official site, onTouchEvent() is described as ...
When a user places one or more fingers on the screen, this triggers the callback onTouchEvent() on the View that received the touch events. For each sequence of touch events (position, pressure, size, addition of another finger, etc.) that is ultimately identified as a gesture, onTouchEvent() is fired several times.
Here's the official link to you for further help
Hope this helps...

Many events are registered on a single touch in libgdx?

I have a game in which if user touches sound button and that should toggle sound on and off but with every touch many touches are registered. For this reason sound is sometimes off and sometime on.
You are probably using Gdx.input.isTouched();
The problem is that you are using that in your update or render method, and you keep your finger on the screen for more than 1 frame, and the audio changes very fast.
In your show method (or create) use
Gdx.input.setInputProcessor(new InputProcessor(...) );
And in the TouchDown method add your code, because that function is called just once whenever the touch is down, not every frame you are touching your screen.

How to detect draglike behavior without ACTION_DOWN and ACTION_UP

My problem concerns onTouch. As far as I can tell, there is no way to detect an ACTION_DOWN for one view, and than detect the ACTION_UP for another view at the same onTouch event or swipe, as every onTouch event is linked to one view.
I have a view (A) with some graphics, and on EVENT_DOWN the coordinates are detected and make a "popup" that consists of a view (B) that ownes some more views ("buttons"). B's position and size may vary form time to time. I would prefer to detect ACTION_UP on one of B's children (the "buttons").
I guess using onInterceptTouchEvent is no good. After all, B is not A's parent. Its the other way around.
Using androids drag-and-drop functionality seems a bit too much. Looks like it's intended for actually dragging graphics and transfering data. Dragging invisible graphics around to detect the finger leaving the screen is not very elegant.
Another way would be detecting B's children's positions (and size), every time the popup is shown, but that is not very smooth either.
What is the best way to detect a view A's ACTION_DOWN, and then its child B's (or it's childrens) ACTION_UP?
Or are there other ways to detect these events?
I did struggle with something very similar for a day or two, and I finally concluded that as far as I know onInterceptTouchEvent is the only way to go. I just created a container view 'C' for A and B, and then when A received an ACTION_DOWN, I instructed 'C' to intercept all further events in the swipe, test the coordinates against B's known location, and then forward the event to 'B' through a custom method if the ACTION_UP tested positive against B's bounds (be careful manually passing MotionEvents from one class to another, as the coordinates will be all wrong).
Not a very satisfying solution, but it was the only thing I could think of to do.
Also just in case you're using it, android 3.0+ has some drag/drop helper methods which I'm sure are very helpful, though I was developing for 2.x so they were useless to me.

How to differentiate between sliding and clicking in android?

In pure Java there is MouseInputListener which I can use to work with those 2 events.
How do I do it with Android?
If I implement both events then only one is fired (onClickListener) and not the other.
Updated:
The question is not about detecting the finger movement.
I have a View (ImageView for example). I need to detect the click on this view which is onClickListener() and finger movement on this view (i.e. press, move then release the finger).
The problem here is that only onClickListener() is called and MotionEvent handler is not caught.
I need to be able to differentiate those 2 events as the main event should be finger movement and onClickListener() should just say "Don't click this view. Spin this view."
Hopefully this is more clear.
OnClickListener and OnTouchListener kinda obstruct each other, since they both consume the MotionEvents that get caught on the View.
Basically you can write a single OnTouchListener that checks for both things. You'll get supplied with the MotionEvent as an argument. Check it's action via MotionEvent.getAction(), e.g. if it equals MotionEvent.ACTION_DOWN (the user put the finger on the display). If the user releases the finger at approx. the same position (ACTION_UP), you may want to interpret that as a click. Otherwise interpret the positions that you get with the ACTION_MOVE event as a gesture.
But the framework already has some classes that do this interpreting work for you, check out the SimpleGestureDetector class and it's SimpleOnGestureListener. That has some callbacks for common events, e.g. onSingleTapConfirmed() and onFling(). All you need to do is supply the MotionEvents from your OnTouchListener to the GestureDetector.

How to implement drag-and-drop between lists in android?

I'm trying to make it so that the user can drag a textview from one listview and then drop it into another, and I'm finding it to be very difficult.
The biggest problem I have found so far is that onTouchEvents seem to only be heard in the view that the ACTION_DOWN event originated in. I'll click in one list and the ACTION_DOWN is heard in there. Then I'll drag outside the list and let go over another list. But the onTouchEvent is only being called for the original list, no matter where I'm going. I thought it was going to be as simple as listening for an ACTION_UP event in the receiving list; but that's called on the original list even though I'm outside it.
What I have so far is a bit messy. When onLongTouchEvent is called, the list tells the main activity to start dragging a draggableTextView (which has a moveTo method). Since the move events continue to be called on the original list, I have it continually setting the position of this draggableTextView. But I can't insert it into another list because I can't figure out what I'm letting go on top of.
Yes, you are right, this is not an easy task. However it is possible: you might check commonsguy's cwac-touchlist example.

Categories