如何捕获ViewFlipper的翻转事件

时间:2015-02-16 13:01:50

标签: android listener viewflipper

我自动翻转的ViewFlipper组件,我希望处理该组件在当前视图和下一个视图之间进行转换的事件。有可能吗?

感谢。

mViewFlipper.setInAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_in));
mViewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this, android.R.anim.fade_out));
mViewFlipper.getAnimation().setAnimationListener(this);
mViewFlipper.startFlipping();

这是日志:

02-16 14:27:50.860:E / AndroidRuntime(7961):FATAL EXCEPTION:main 02-16 14:27:50.860:E / AndroidRuntime(7961):进程:com.idiadasample,PID:7961 02- 16 14:27:50.860:E / AndroidRuntime(7961):java.lang.RuntimeException:无法启动活动ComponentInfo {com.idiadasample / com.idiada.activity.ViewFlipperActivity}:java.lang.NullPointerException:尝试调用虚方法' void android.view.animation.Animation.setAnimationListener(android.view.animation.Animation $ AnimationListener)'在null对象引用02-16 14:27:50.860:E / AndroidRuntime(7961):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2298)02-16 14:27:50.860:E / AndroidRuntime(7961) ):在android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2360)02-16 14:27:50.860:E / AndroidRuntime(7961):在android.app.ActivityThread.access $ 800(ActivityThread.java:144)02 -16 14:27:50.860:E / AndroidRuntime(7961):在android.app.ActivityThread $ H.handleMessage(ActivityThread.java:1278)02-16 14:27:50.860:E / AndroidRuntime(7961):at android .os.Handler.dispatchMessage(Handler.java:102)02-16 14:27:50.860:E / AndroidRuntime(7961):at android.os.Looper.loop(Looper.java:135)02-16 14:27 :50.860:E / AndroidRuntime(7961):在android.app.ActivityThread.main(ActivityThread.java:5221)02-16 14:27:50.860:E / AndroidRuntime(7961):at java.lang.reflect.Method。 invoke(Native Method)02-16 14:27:50.860:E / AndroidRuntime(7961):at java.lang.reflect.Method.invoke(Method.java:372)02-16 14:27:50.860:E / AndroidRuntime(7961):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:899)02-16 14:27:50.860:E / AndroidRuntime(7961):at com.android.internal.os .ZygoteInit.main(ZygoteInit.java:694)02-16 14:27:50.860:E / AndroidRuntime(7961):引起:java.lang.NullPointerException:尝试调用虚方法' void android.view。 animation.Animation.setAnimationListener(android.view.animation.Animation $ AnimationListener)'在null对象引用02-16 14:27:50.860:E / AndroidRuntime(7961):at com.idiada.activity.ViewFlipperActivity.onCreate(ViewFlipperActivity.java:89)02-16 14:27:50.860:E / AndroidRuntime (7961):在android.app.Activity.performCreate(Activity.java:5933)02-16 14:27:50.860:E / AndroidRuntime(7961):at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105) 02-16 14:27:50.860:E / AndroidRuntime(7961):在android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2251)02-16 14:27:50.860:E / AndroidRuntime(7961):... 10多个

2 个答案:

答案 0 :(得分:4)

您可以按this answer

中的建议添加动画并收听动画事件
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
   public void onAnimationStart(Animation animation) {}
   public void onAnimationRepeat(Animation animation) {}
   public void onAnimationEnd(Animation animation) {}
});

设置动画:

viewFlipper.setInAnimation(AnimationUtils.loadAnimation(this,
         R.anim.fade_in));
viewFlipper.setOutAnimation(AnimationUtils.loadAnimation(this,
         R.anim.fade_out));

答案 1 :(得分:0)

我已经延长ViewFlipper来做到这一点。

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ViewFlipper;

public class DecentViewFlipper extends ViewFlipper implements View.OnLayoutChangeListener {

    public void onLayoutChange(View view, int i, int i1, int i2, int i3, int i4, int i5, int i6, int i7) {
        onViewFlipperFlippedListener.onViewFlipperFlipped(view, getDisplayedChild());
    }

    public interface OnViewFlipperFlippedListener {
        void onViewFlipperFlipped(View childView, int index);
    }

    public DecentViewFlipper(Context context) {
        super(context);
    }

    public DecentViewFlipper(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    private static final String TAG = DecentViewFlipper.class.getSimpleName();
    private OnViewFlipperFlippedListener onViewFlipperFlippedListener = null;

    public void setOnViewFlipperFlippedListener(@NonNull OnViewFlipperFlippedListener listener){
        if (listener == null) throw new IllegalStateException("OnViewFlipperFlippedListener can not be null");
        if (getChildCount() != 0) {
            DecentViewFlipper flipper = (DecentViewFlipper) getCurrentView().getParent();
            flipper.addOnLayoutChangeListener(this);
        } else {
            throw new IllegalStateException("can't use setOnViewFlipperFlippedListener before you have added at least one View to the "+TAG);
        }
        onViewFlipperFlippedListener = listener;
    }

    public OnViewFlipperFlippedListener getOnViewFlipperFlippedListener() {
        return onViewFlipperFlippedListener;
    }

}