3

This is maddening. I have the following XML layout:

<FrameLayout android:layout_width="fill_parent" android:layout_height="wrap_content" android:background="@drawable/shadow" android:focusable="true" android:focusableInTouchMode="true">
    <ViewFlipper android:id="@+id/flipper" android:layout_width="fill_parent" android:layout_height="fill_parent">
        <EditText android:id="@+id/reviews" style="@style/DescriptionArea" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:background="@null" />
     <EditText android:id="@+id/notes" style="@style/DescriptionArea" android:hint="@string/detail_hint" android:layout_width="fill_parent" android:layout_height="wrap_content" android:enabled="false" android:maxLines="4"/>
    </ViewFlipper>
</FrameLayout>

And the Java:

viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
    slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
    slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
    slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
    slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);

    gestureDetector = new GestureDetector(new MyGestureDetector());
    gestureListener = new View.OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }
    };

It seems like if I try to fling on top of the EditText area, the gesture does not register. However, if I fling around it, that is, the background of the EditText, it DOES work. I've tried following around with the fill_parent/wrap_content heights and widths, but it doesn't seem to change a thing. I've confirmed this suspicion my making the EditText background "red," and noting that nothing within that red rectangle can activate a fling. So how do I make this work?

2 Answers2

4

You may override activity's dispatchTouchEvent method:

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    if (gestureDetector != null) {
        gestureDetector.onTouchEvent(ev);
    }
    return super.dispatchTouchEvent(ev);
}

Obviously gestureDetector is a member variable you need to declare and initialize on your activity.

Konstantin Burov
  • 68,980
  • 16
  • 115
  • 93
  • How terrible! I was only overriding onTouchEvent: @Override public boolean onTouchEvent(MotionEvent event) { if (gestureDetector.onTouchEvent(event)) return true; else return false; } Thanks for your help. Would upvote a hundred times if I could. –  Aug 22 '10 at 18:29
  • How about my case ? I have a layout with a tabcontrol with two tabsheets. one of them has a viewflipper. this viewflipper has some edittexts, buttons, etc inside it's layout. My question is, how can I control the gesture detector to activate only if the user move the finger inside my viewflipper ? I mean ... if I do what you said, the user can fling without while can't see the viewflipper ... I tried to put a focusable layout but it's "onTouch" is not handle, even if i set focusability descendent to "focus before descendents" – Leandro Silva Oct 08 '12 at 18:14
  • @digulino well, I guess you have to inspect the MotionEvent parameter and decide if it target your view flipper or not. – Konstantin Burov Oct 09 '12 at 07:42
  • thx for the answer, well ...the fling I can perform OK, but i only want to perform it if the user's moviment is performed 'inside' the viewflipper. Here's an example: a layout with 50% button and 50% viewflipper. I want that the fling is performed only if the moviment has done inside the viewflipper, not inside the button. In resume, my problem is I can't know how to hold the focus when the user touches the viewflipper. – Leandro Silva Oct 09 '12 at 14:23
1

try to apply setLongClickable(true) for the ViewFlipper

JMax
  • 26,109
  • 12
  • 69
  • 88
jakk
  • 11
  • 1