Java ViewFlipper-点击图像时出错
我的可视翻转器有个奇怪的问题。除了一种特殊情况外,它工作得很好。当我第一次打开活动并点击第一个图像而不是滑动它时,应用程序显示错误。但当我滑动它的作品很好,并显示下一张图片。之后,当我返回到第一个图像并再次点击它时,它工作正常 我的java代码:Java ViewFlipper-点击图像时出错,java,android,viewflipper,Java,Android,Viewflipper,我的可视翻转器有个奇怪的问题。除了一种特殊情况外,它工作得很好。当我第一次打开活动并点击第一个图像而不是滑动它时,应用程序显示错误。但当我滑动它的作品很好,并显示下一张图片。之后,当我返回到第一个图像并再次点击它时,它工作正常 我的java代码: import android.app.Activity; import android.os.Bundle; import android.view.MotionEvent; import android.view.View; import andro
import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.widget.ViewFlipper;
public class MyActivity extends Activity {
ViewFlipper my_flipper;
private float lastX;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my_activity);
my_flipper = (ViewFlipper) findViewById(R.id.my_flipper);
showImages();
}
public void showImages () {
my_flipper.setDisplayedChild(0);
my_flipper.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View view, MotionEvent motionEvent) {
switch (motionEvent.getAction()) {
case MotionEvent.ACTION_DOWN:
lastX = motionEvent.getX();
return true;
case MotionEvent.ACTION_UP:
float currentX = motionEvent.getX();
if (lastX < currentX) {
if (my_flipper.getDisplayedChild() == 0)
break;
my_flipper.setInAnimation(getApplication(), R.anim.slide_in_from_left);
my_flipper.setOutAnimation(getApplication(), R.anim.slide_out_to_right);
my_flipper.showNext();
}
if (lastX > currentX) {
if (my_flipper.getDisplayedChild() == 1) {
break;
}
my_flipper.setInAnimation(getApplication(), R.anim.slide_in_from_right);
my_flipper.setOutAnimation(getApplication(), R.anim.slide_out_to_left);
my_flipper.showPrevious();
}
my_flipper.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {
}
public void onAnimationRepeat(Animation animation) {
}
public void onAnimationEnd(Animation animation) {
}
});
}
return true;
}
});
}
}
如果您能提供任何帮助,我将不胜感激。好的。我想我找到了解决办法。我将
if(lastX
替换为if(lastX<=currentX)
。好的。我想我找到了解决办法。我将if(lastX
替换为if(lastX<=currentX)
。
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:background="#0f4b75"
tools:context=".MyActivity">
<ViewFlipper
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/my_flipper"
android:background="#000000"
>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/atrakcja_0"
android:adjustViewBounds="true"
android:src="@drawable/foto"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/atrakcja_1"
android:adjustViewBounds="true"
android:src="@drawable/foto"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/atrakcja_2"
android:adjustViewBounds="true"
android:src="@drawable/foto"
/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/atrakcja_3"
android:adjustViewBounds="true"
android:src="@drawable/foto"
/>
</ViewFlipper>
</RelativeLayout>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false">
<translate
android:fromXDelta="-100%" android:toXDelta="0%"
android:fromYDelta="0%" android:toYDelta="0%"
android:duration="140" />
</set>
my_flipper.getOutAnimation().setAnimationListener(new Animation.AnimationListener() {