我在 viewSwitcher 中有 2 个布局,我需要在滑动时进行更改。
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;
}
}
我也是java和Android的新手。请帮助我,我在哪里做错了?我得到错误switcher.showNext()
和switcher.showPrevious()
。
请帮忙。我的例子是使用 android 4.0.3,但我需要在 2.1 上工作。我不知道如何解决这个问题。
viewSwitcher 可以在 Android 2.1 上运行吗?
我的 XML 文件。
<?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>