Named ViewFlipper : ViewFlipper « UI « Android






Named ViewFlipper

 

/**
 * Copyright (c) 2011 Axa Holding Belgium, SA. All rights reserved.
 * This software is the confidential and proprietary information of the AXA Group.
 */

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import android.content.Context;
import android.util.AttributeSet;
import android.view.View;
import android.widget.ViewFlipper;
interface iViewFlipperListener{
  void viewChangedTo(String name);
}
public class NamedViewFlipper extends ViewFlipper {
  
  private Map<View, String> myViewNames = new HashMap<View, String>();
  private List<iViewFlipperListener> myListeners = new ArrayList<iViewFlipperListener>();

  public NamedViewFlipper( Context aContext ) {
    super( aContext );
  }

  public NamedViewFlipper( Context aContext, AttributeSet aAttrs ) {
    super( aContext, aAttrs );
  }

  public void setNameForView(View aView, String aName){
    myViewNames.put(aView, aName);
  }
  
  public String getCurrentViewName(){
    return myViewNames.get(getCurrentView());
  }
  
  public void flipToView(String aName){
    while(!getCurrentViewName().equals( aName )){
      super.showNext();
    }
    notifyListeners();
  }

  @Override
  public void showNext() {
    super.showNext();
    notifyListeners();
  }

  @Override
  public void showPrevious() {
    super.showPrevious();
    notifyListeners();
  }
  
  @Override
  public void setDisplayedChild( int aWhichChild ) {
    super.setDisplayedChild( aWhichChild );
    notifyListeners();
  }
  
  private void notifyListeners() {
    String theCurrentViewName = getCurrentViewName();
    for(iViewFlipperListener theListener : myListeners){
      theListener.viewChangedTo( theCurrentViewName );
    }
  }
  
  public void addViewFlipperListener(iViewFlipperListener aListener){
    myListeners.add(aListener);
  }
  
  public void removeViewFlipperListener(iViewFlipperListener aListener){
    myListeners.remove( aListener );
  }

}

   
  








Related examples in the same category

1.Using ViewFlipper