views:

503

answers:

1

Hi Android gurus,

I have a ViewFlipper for which I want a listener to fire when the child displayed is changed. I have set an OnFocusChangeListener to the ViewFlipper but it never fires when I flip from child 0 to child 1 or vice-versa. The ViewFlipper contains two RelativeLayouts and I have tried setting 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 a function I use to show, well, popups.

Does anyone have working code for some sort of trigger that fires when a ViewFlipper changes which child is displayed?

+1  A: 

I think you don't understand what "focus" means on Android; it's when a user selects that View, for example when they select an EditText by pressing on it, or they use the keyboard to select a button. When a ViewFlipper switches Views, it does not gain or lose focus.

As far as I know, you can either:

  1. Don't use ViewFlipper's automatic flipping, instead manually calling showNext() when you want it to flip (then setting the interval yourself in code).

  2. Subclass or reimplement ViewFlipper and add a change listener to your liking.

Daniel Lew