yuttadhammo
yuttadhammo

Reputation: 5199

How to detect fling on TextView without cancelling scroll?

Trying to detect a fling on a TextView using simpleGestureDetector:

public void onCreate(Bundle savedInstanceState) {

    ...

    textContent.setOnTouchListener(new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent ev) {
                return gestureDetector.onTouchEvent(ev);
        }        
    });
}

class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

            if(foo) {
                fooBar();
                return true;
            }
        return super.onFling(e1, e2, velocityX, velocityY);
    }
} 

This works in the sense that my code fooBar() is run if the conditions foo are met, but the problem I find is that when the conditions aren't met, returning super.onFling() causes the fling to be cancelled, it seems, since the textview scroll no longer continues after the finger is lifted on a fling. Here is the XML:

<RelativeLayout
    android:id="@+id/main_area"
    android:layout_height="match_parent"
    android:layout_width="match_parent">
    <TextView
        android:id="@+id/main_text"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:padding="10dp"
        android:textSize="16dp"
        android:lineSpacingExtra="2dip"
    />
</RelativeLayout>

Is there any way I can keep the ordinary scroll behaviour and still detect the fling?

EDIT: Sorry, my bad, this had nothing to do with the gesture detector, it was just because I took the TextView out of its ScrollView - for some reason it seems TextViews don't have smooth (inertia) scrolling like ScrollViews.

Upvotes: 1

Views: 1759

Answers (2)

Veger
Veger

Reputation: 37905

The difference between scroll and fling events is that the user lifts his finger in the end of the movement in order to make it a fling. (and the speed is higher).

Therefore, I think it is not possible to combine both events as they are (too) similar to detect (before ending them) which one is being performed. Also from a user perspective, using both gestures at the same time is confusing (due to the similarity).

So the answer is: no there is no way to keep the ordinary scroll behaviour and still detect the fling.

(at least I tried in the past and did not succeed in using both events in a proper way)

Upvotes: 2

Zolt&#225;n
Zolt&#225;n

Reputation: 22156

I'm not sure this will be your problem, but why do you return true if your condition is met? If you don't want the event to be consumed, you should return false (or super.onFling). That's what the return value is for.

Upvotes: 0

Related Questions