结合来自ViewFlipper 小部件翻转事件的 Listener的答案
和
安卓; 在 AdapterViewFlipper 上设置输入/输出动画:未知的动画师名称翻译
您可以在 AdapterViewFlipper 上设置一个Animator(注意这与 ViewFlipper 中使用的动画不同)然后您可以创建一个 AnimatorListener 并将其添加到“In Animation”中,它会在显示子项时触发,然后您可以调用getDisplayedChild() 返回索引。
示例代码
// Setup AdapterViewFlipper
mAdapterViewFlipper = (AdapterViewFlipper) findViewById(R.id.AdapterViewFlipper);
mAdapterViewFlipper.setFlipInterval(5000);
// Set Some Animations
mAdapterViewFlipper.setInAnimation(getBaseContext(), R.animator.left_in);
mAdapterViewFlipper.setOutAnimation(getBaseContext(), R.animator.right_out);
// Create a AnimatorListener
mAnimatorListener = new AnimatorListener() {
public void onAnimationStart(Animator animator) {}
public void onAnimationRepeat(Animator animator) {}
public void onAnimationCancel(Animator animator) {}
public void onAnimationEnd(Animator animator) {
int displayedChild = mAdapterViewFlipper.getDisplayedChild();
Log.w(TAG, "onAnimationEnd view index:" + displayedChild);
int childCount = mAdapterViewFlipper.getCount();
if (displayedChild == childCount - 1) {
// Do what you want before wrapping to first view
//mAdapterViewFlipper.stopFlipping();
}
}
};
// Add the Listener to the In animation
mAdapterViewFlipper.getInAnimation().addListener(mAnimatorListener);
// Set adaptor just before start so view with index 0 triggers Listener
mAdapterViewFlipper.setAdapter(adapter);
// Start Flipping
mAdapterViewFlipper.setAutoStart(true);
R.animator.left_in 和 R.animator.right_out 是“res/animator”目录中的文件,内容如下:-
<?xml version="1.0" encoding="utf-8"?>
<objectAnimator xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator"
android:propertyName="x"
android:valueType="floatType"
android:valueFrom="-1500"
android:valueTo="0"
android:duration="600"/>
每次将视图翻转给您时,您都会在此示例中收到一个 Log 事件,例如:-
TAG onAnimationEnd view index:0
TAG onAnimationEnd view index:1
TAG onAnimationEnd view index:2
TAG onAnimationEnd view index:3
TAG onAnimationEnd view index:4
TAG onAnimationEnd view index:0