I would like to use viewSwitcher to switch between two layouts. It works with a button, but I would like to switch with touching the screen. I've found an onTouchListener example and i tried to implemet some kind of similar, but I did not manage to.
In my main.xml i got one Linear Layout which contains a ViewSwitcher and two other LinearLayout. These are the layouts i want to switch.
here it is:
<ViewSwitcher
android:id="@+id/viewSwitcher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inAnimation="@android:anim/slide_in_left"
android:outAnimation="@android:anim/slide_out_right" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/firstlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/bg"
android:orientation="vertical" >
<ImageView
android:id="@+id/title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="@drawable/titlebar" />
<Button
android:id="@+id/somebut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="30px"
android:background="@drawable/but" />
</LinearLayout>
<LinearLayout
android:id="@+id/secondlayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second layout" >
</TextView>
</LinearLayout>
</ViewSwitcher>
and my code:
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;
import android.widget.ViewSwitcher;
public class SwitcherActivity extends Activity {
private float downXValue;
private View firstView;
private View SecondView;
private ViewSwitcher vs;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
vs = (ViewSwitcher)findViewById(R.id.viewSwitcher);
firstView= findViewById(R.id.firstlayout);
secondView = findViewById(R.id.secondlayout);
LinearLayout first = (LinearLayout) findViewById(R.id.firstlayout);
first.setOnTouchListener((OnTouchListener) this);
}
public boolean onTouch(View arg0, MotionEvent arg1) {
switch (arg1.getAction()){
case MotionEvent.ACTION_DOWN:{
// store the X value when the user's finger was pressed down
downXValue = arg1.getX();
break;
}
case MotionEvent.ACTION_UP:{
// Get the X value when the user released his/her finger
float currentX = arg1.getX();
// going backwards: pushing stuff to the right
if (downXValue < currentX)
{
if (vs.getCurrentView() != myFirstView)vs.showPrevious();
}
// going forwards: pushing stuff to the left
if (downXValue > currentX)
{
if (vs.getCurrentView() != mySecondView)
vs.showNext();
}
break;
}
}
return true;
}
}
My problem is, that i got ClassCastException in the line which contains the following:
first.setOnTouchListener((OnTouchListener) this);
Your activity has to implement OnTouchListener so that "this" can be used as the listener. Right now it's trying to cast an Activity to an OnTouchListener which is what's throwing the ClassCastException.
You also need to override the OnTouchListener methods to customize the way the touches are handled.