Android: how to set a listener that fires when my ViewFlipper shows a new child
I have a ViewFlipper for which I want a listener to fire when the displayed child changes. I have set OnFocusChangeListener to ViewFlipper, but it never fires when I flip from child 0 to child 1 or vice versa. The ViewFlipper contains two RelativeLayouts and I tried to set OnFocusChangeListeners for those, but I get a ClassCastException when I try to set it. Here's my code:
RelativeLayout songsLayout = (RelativeLayout) findViewById(R.id.song_page_layout);
songsLayout.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View view, boolean hasFocus) {
showPopUp("View " + view.getId() + " now has focus: " + hasFocus);
}
} );
R.is.song_page_layout is one of my RelativeLayouts, and showPopUp () is the function I use to show, well, popups.
Does anyone have a working code for some kind of trigger that fires when the ViewFlipper changes which child is displayed?
a source to share
I don't think you understand what "focus" means on Android; this is when the user selects this view, for example when they select an EditText by clicking on it, or they use the keyboard to select a button. When the ViewFlipper switches Views, it doesn't gain or lose focus.
As far as I know, you can:
-
Don't use the automatic toggling ViewFlipper, instead manually call showNext () when you want to flip it (then set the spacing yourself in code).
-
Subclass or override ViewFlipper and add a change listener to your liking.
a source to share