How onFling works in viewSwitcher

2.1k Views Asked by At

I have 2 layouts inside a viewSwitcher, I need to change when I swipe that.

package slide.trys.one;

import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.widget.ViewSwitcher;

public class SlideActivity extends Activity implements OnGestureListener {

private ViewSwitcher switcher;
private GestureDetector gesturedetector = null;
@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    gesturedetector = new GestureDetector(this, this);

}

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


int SWIPE_MIN_VELOCITY = 100;
int SWIPE_MIN_DISTANCE = 100;

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {

    float ev1X = e1.getX();
    float ev2X = e2.getX();

    final float xdistance = Math.abs(ev1X - ev2X);

    final float xvelocity = Math.abs(velocityX);

    if( (xvelocity > SWIPE_MIN_VELOCITY) && (xdistance > SWIPE_MIN_DISTANCE) )
    {
        if(ev1X > ev2X)
        {
            switcher.showNext(); //Error in this part
        }
        else
        {
            switcher.showPrevious(); //Error in this part
        }
    }

    return false;
}

public void onLongPress(MotionEvent e) {

}

public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,float distanceY) {
    return false;
}

public void onShowPress(MotionEvent e) {

}

public boolean onSingleTapUp(MotionEvent e) {
    return false;
}

public boolean onDown(MotionEvent e) {
    return false;
}

}

I'm a newbie for java and Android too. pls help me, where am I doing mistake ? I getting error with switcher.showNext() and switcher.showPrevious() .

pls help. the example I have is using android 4.0.3, But I need to work on 2.1. I don't know how to fix this.

Will the viewSwitcher works on Android 2.1 ??

my XML file.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

<ViewSwitcher
    android:id="@+id/viewSwitcher"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:inAnimation="@anim/in_animation"
    android:outAnimation="@anim/out_animation" >

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
            android:id="@+id/imageView2"
            android:contentDescription="@string/app_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:src="@drawable/footer" />

    </RelativeLayout>

    <RelativeLayout
        android:layout_width="fill_parent"
        android:layout_height="fill_parent" >

        <ImageView
           android:id="@+id/imageView1"
           android:contentDescription="@string/app_name"
           android:layout_width="wrap_content"
           android:layout_height="wrap_content"
           android:src="@drawable/blue_header" />

    </RelativeLayout>        

    </ViewSwitcher>

</LinearLayout>
1

There are 1 best solutions below

3
XMight On

Use ViewFlipper better for switching between layouts.

First of all, understand the difference between ViewFlipper and ViewSwitcher. Also, for 2.1, in the case of ViewFlipper, there is a bug present(up to Android 2.3.4), the workaround is here:

http://daniel-codes.blogspot.com/2010/05/viewflipper-receiver-not-registered.html

So, you extend ViewFlipper and do the workaround for the bug, and in xml you access your CustomViewFlipper like that:

<com.mypackage.MyViewFlipper
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/news_layout_flipper"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
    <LinearLayout
        android:id="@+id/first_to_show">
    </LinearLayout>
    <LinearLayout
        android:id="@+id/second_to_show">
    </LinearLayout>
</com.mypackage.MyViewFlipper>