0

I have a flipper:

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:id="@+id/ParentLayout"
    xmlns:android="http://schemas.android.com/apk/res/android" style="@style/MainLayout" >
            <LinearLayout android:id="@+id/FlipperLayout" style="@style/FlipperLayout">
                <ViewFlipper android:id="@+id/viewflipper" style="@style/ViewFlipper">
                    <!--adding views to ViewFlipper-->
                    <include layout="@layout/home1" android:layout_gravity="center_horizontal" />
                    <include layout="@layout/home2" android:layout_gravity="center_horizontal" />
                </ViewFlipper>
            </LinearLayout>
</LinearLayout>

The first layout,home1, consists of a scroll view. What should I do to distinguish between the flipping gesture and the scrolling? Presently:

  • if I remove the scroll view, I can swipe across
  • if I add the scroll view, I can only scroll.

I saw a suggestion that I should override onInterceptTouchEvent(MotionEvent), but I do not know how to do this. My code, at this moment, looks like this:

public class HomeActivity extends Activity {
-- declares
@Override
public void onCreate(Bundle savedInstanceState) {
    -- declares & preliminary actions

    LinearLayout layout = (LinearLayout) findViewById(R.id.ParentLayout);
    layout.setOnTouchListener(new OnTouchListener() {
        public boolean onTouch(View v, MotionEvent event) {
            if (gestureDetector.onTouchEvent(event)) {
                return true;
            }
            return false;
        }});

@Override 
public boolean onTouchEvent(MotionEvent event) { 
    gestureDetector.onTouchEvent(event); 
    return true;
    }
class MyGestureDetector extends SimpleOnGestureListener {
    @Override
    public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
    // http://www.codeshogun.com/blog/2009/04/16/how-to-implement-swipe-action-in-android/
    }
}

}

Can anybody please guide me in the right direction?

Thank you.

Manu
  • 1,013
  • 1
  • 11
  • 20

3 Answers3

1

The view flipper only displays one view at a time as explained here. It is a kind of switch that is very useful when the developer wants to give the user a choice as to how to view the data (list, thumbnails, ect). therefore, the reason why you cant scroll and fling at the same time is because one view only scrolls and the other view only flings and only one is open at a time.

If you want your display to both scroll and fling, you will have to design a layout that is capable of both and override the needed methods. The first step towards that would be to remove your ViewFLipper and use something else.

Hope this was helpful!

mtmurdock
  • 12,756
  • 21
  • 65
  • 108
  • Thank you for your very quick answer. As a matter of fact I was thinking of a way to capture the motion event at the ParentLayout level (the one that encloses the flipper) and decide at that level whether the gesture is a horizontal one (to be taken into account by the flipper) or a vertical one (to be passed to the scroll view). That's why the onInterceptTouchEvent seemed to me the right one. So there is no way to achieve what I had in mind? – Manu Jun 13 '10 at 15:14
  • I guess perhaps im not entirely sure what you are trying to achieve. what are home1 and home2, and what is each gesture supposed to accomplish? what is your app supposed to do? – mtmurdock Jun 13 '10 at 18:33
  • The home1 and home2 layouts contain each a number of image buttons, each image button triggers an activity. home1 and home2 are included in a layout called home, the one hosting the flipper. Now: when the user swipes accross I need to display the next home layout, which is done by the flipper. But since their are many image buttons, when the phone is in landscape mode they do not fit on screen all of them, so I need to scroll, the home1 layout. – Manu Jun 16 '10 at 08:13
  • ok i get it. well there is no reason why you shouldnt be able to do that. i think you're layout is probably right, you just need to properly implement the gesture code. i think you're on the right track too, but i dont know anything about gestures... sorry. – mtmurdock Jun 16 '10 at 12:44
1

Basically, if you have a ScrollView in a ViewFlipper, all you have to do is attach onTouchListener to the ScrollView:

    ScrollView scrollView = (ScrollView) findViewById(R.id.scrollView);        
    scrollView.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event){
            if (myGestureDetector.onTouchEvent(event)){
                return true;
            }
            else{   
                return false;
            }
        }
    });
Yar
  • 4,543
  • 2
  • 35
  • 42
0

I try this way to make it works:

@Override
public boolean onTouchEvent(MotionEvent event) 
{
    scrollView.onTouchEvent(event); 
    return super.onTouchEvent(event);
}

@Override
public boolean dispatchTouchEvent(MotionEvent e)
{
    gestureDetector.onTouchEvent(e);
    return super.dispatchTouchEvent(e);
}
Richard
  • 339
  • 3
  • 10