I have a ViewFlipper implementation that needs to be improved. This ViewFlipper has three child views. Basically, I want an indicator on which child view is currently active. My ViewFlipper is just a part of a complex layout which also has list views, etc.
Switching of views is also automatic and done in a specified interval.
From Android's SDK reference, I haven't seen any listener for when the ViewFlipper changes the child view.
Do you guys know of a way I can have a listener for that event?
Or are there alternative ways I can implement this feature besides using ViewFlipper ?
Thanks!
If you apply animation (out or in animation) on view switching, you can set listener to an animation and, for example, act on animation end.
viewFlipper.getInAnimation().setAnimationListener(new Animation.AnimationListener() {
public void onAnimationStart(Animation animation) {}
public void onAnimationRepeat(Animation animation) {}
public void onAnimationEnd(Animation animation) {}
});
I find one way to detect which child is actived :
addOnLayoutChangeListener to ViewFlipper, and getCurrentView of ViewFlipper, then compare with childs of ViewFlipper.
remember to removeOnLayoutChangeListener when activity onDestory
private View page1, page2, page3, page4;
private ViewFlipper viewFlipper;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.flipper);
page1 = findViewById(R.id.MyFlipper_page01);
page2 = findViewById(R.id.MyFlipper_page02);
page3 = findViewById(R.id.MyFlipper_page03);
page4 = findViewById(R.id.MyFlipper_page04);
viewFlipper = (ViewFlipper) findViewById(R.id.MyFlipper_flipper);
viewFlipper.addOnLayoutChangeListener(onLayoutChangeListener_viewFlipper);
}
View.OnLayoutChangeListener onLayoutChangeListener_viewFlipper = new View.OnLayoutChangeListener() {
@Override
public void onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom) {
if(viewFlipper.getCurrentView() == page1)
Log.d("test", "change to flipper_page1");
else if(viewFlipper.getCurrentView() == page2)
Log.d("test", "change to flipper_page2");
else if(viewFlipper.getCurrentView() == page3)
Log.d("test", "change to flipper_page3");
else if(viewFlipper.getCurrentView() == page4)
Log.d("test", "change to flipper_page4");
}
};
@Override
protected void onDestroy() {
super.onDestroy();
viewFlipper.removeOnLayoutChangeListener(onLayoutChangeListener_viewFlipper);
}
I have created an extended ViewFlipper which does exactly that: DecentViewFlipper
I know this question already has an answer. But here's an alternative which is a method ViewFlipper
inherited from ViewGroup
and which seems to be the best from my experience.
Code Snippets Below:
ViewFlipper viewFlipper = findViewById (R.id.myViewFlipperId);
viewFlipper.setOnHierarchyChangeListener(ViewGroup.OnHierarchyChangeListener);
The onChildViewAdded(View, View)
method in the callback listener will be invoked whenever a child view is added to the ViewGroup
.
Which you can use to detect whenever the ViewFlipper
flips.
While this is an old question I found a decent approach that works.
public class MainLaunch extends Activity {
... main setup and code
int currentIndex = 0;
int maxIndex = 3;
// set specific animations for the view flipper on showNext
// only showNext while in valid index
public void showNext() {
if( currentIndex < maxIndex )
{
currentIndex++;
viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_left);
viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_right);
viewFlipper.showNext();
}
}
// set specific animations for the view flipper on showPrevious
// only showPrevious while in valid index
public void showPrevious() {
if( currentIndex > 0 )
{
currentIndex--;
viewFlipper.setInAnimation(getBaseContext(), R.anim.slide_in_right);
viewFlipper.setOutAnimation(getBaseContext(), R.anim.slide_out_left);
viewFlipper.showPrevious();
}
}
// get current flipped view
public View getCurrentView() {
return viewFlipper.getChildAt(currentIndex);
}
}
Then to use the ViewFlipper you call showNext() or showPrevious anywhere and can get the currently active view by calling getCurrentView(). This helps in setting different animations for left and right flipping and for easily getting current working views.
来源:https://stackoverflow.com/questions/3813108/listener-for-viewflipper-widget-flipping-events