我在viewSwitcher中有2個佈局,當我滑動時需要更改。viewSwitcher中onFling的工作方式
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>
你必須在這裏發表您的佈局,以便別人找到你的錯誤,也許是在XML – XMight 2012-07-18 09:10:31