Question

In my viewFlipper some TextViews are loaded dynamically. The size may differ which means that under the viewFlipper there could be some space left (see the green part in the screenshot)

screenshot

I want the onFling method to be called not only when swiping on the grey part (which is the viewflipper) but also when swiping on the green part

my layout looks like this:

<?xml version="1.0" encoding="utf-8"?>

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="@+id/root">


<ViewFlipper android:id="@+id/viewFlipper"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content">       
</ViewFlipper>

 </RelativeLayout>

In my onCreate i do this:

this.viewFlipper = (ViewFlipper) this.findViewById(R.id.viewFlipper);

    this.gestureDetector = new GestureDetector(new MyGestureDetector());

    RelativeLayout root = (RelativeLayout) this.findViewById(R.id.root);
    root.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                Log.d("root", "true");
                return false;
            } else {
                Log.d("root", "false");
                return false;
            }
        }
    });

So far I tried to return false even if I get true, so that the event does not get consumed and gets passed down to the viewFlipper even if the swipe has been made outside the viewflipper.

Note that the viewFlipper does not need any explicit onTouchListener. It works with or without one ( I dont really understand why..)

Does anyone know what to do?

Was it helpful?

Solution

Try adding android:clickable="true" to your RelativeLayout and you should be able to do it.

If that doesn't work you might need to implement these two methods also (if you haven't allready):

@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
    //TouchEvent dispatcher.
    if (gestureDetector != null) {
        if (gestureDetector.onTouchEvent(ev))
            //If the gestureDetector handles the event, a swipe has been executed and no more needs to be done.
            return true;
    }
    return super.dispatchTouchEvent(ev);
}

@Override
public boolean onTouchEvent(MotionEvent event) {
    return gestureDetector.onTouchEvent(event);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top