IdentifiantMot de passe
Loading...
Mot de passe oublié ?Je m'inscris ! (gratuit)
Navigation

Inscrivez-vous gratuitement
pour pouvoir participer, suivre les réponses en temps réel, voter pour les messages, poser vos propres questions et recevoir la newsletter

Android Discussion :

Création d'un Accordeon


Sujet :

Android

  1. #1
    Membre actif

    Inscrit en
    Octobre 2009
    Messages
    133
    Détails du profil
    Informations forums :
    Inscription : Octobre 2009
    Messages : 133
    Points : 295
    Points
    295
    Par défaut Création d'un Accordeon
    Bonjour,

    Dans le cadre d'un développement personnel pour une application Android,
    j'ai cherche a faire un accordéon (un peu comme ça http://jqueryui.com/demos/accordion/ )

    Comme le composant n'existe pas par défaut et que je n'ai rien trouvé répondant a mes besoins sur le net, je l'ai donc recréé directement.

    Bien évidemment, mon code est essentiellement pour moi, il peut donc paraitre un peu spécifique (même si j'ai essayé de le rendre générique pour l'utiliser a d'autres endroits), de même il peut exister quelques problèmes d'optimisation (je n'en suis pas encore arrivé la)
    mais si ça peut aider quelqu'un, je le poste donc ici.

    Tout est donc quasiment dans le constructeur, il faut en entrée :
    - l'activité,
    - le layout dans lequel notre accordéon sera (ce layout doit etre vide)
    - List<java.util.Map<String, String>> mElement : Une liste des éléments a afficher. Sachant que les éléments sont donc un titre et une description, j'ai ici une liste de map contenant 2 key/value (name et description)

    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
     
    /**
     * Component for creating an Accordion
     * and manage it
     */
    public class Accordion {
     
    	// Current ID showed in the layout
    	public int currentId;
     
    	// Description to show
    	public TextView show;
     
    	// Description to hide
    	public TextView hide;
     
        // Constructor
        // We create everything for the accordion here
        // For each element, we will create 2 component
        // - 1 text view for title
        // - 1 text view for description
        // The objective is to show always all names and show/hide wanted descriptions
        public Accordion(Activity activity, final LinearLayout layout, List<java.util.Map<String, String>> mElement) {
     
        	// For being sure we don't cut other ids, we begin at 101 (normally, the layout don't have other things than the accordion
            int id = 100;
            boolean first = true;
     
        	for (java.util.Map<String, String> currentElement : mElement) {
     
        		// Title
            	TextView tvTitle = new TextView(activity);
            	tvTitle.setText( currentElement.get( "name" );
            	tvTitle.setId(id ++);
            	tvTitle.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 1f) );
            	tvTitle.setPadding(1, 1, 1, 0);
            	tvTitle.setBackgroundColor(000000);
     
            	// Click listener for the title
            	// This listener will permit to show/hide the component of the accordion according to the layout and his position
            	tvTitle.setOnClickListener(new Button.OnClickListener() {
     
            		public void onClick(View v) {
     
        				// If we click on the current title, we don't do nothing
        				if ( (v.getId() + 1) == currentId ) {
        					return;
        				}
     
        				// We put an animation for hiding the current description
        				hide = (TextView)layout.findViewById(currentId);
        				AlphaAnimation hiding = new AlphaAnimation(1, 0);
        				hiding.setDuration(1000);
        				hide.setAnimation(hiding);
     
        				// If the current description is before the click one, we will getting up every object between
        				if (currentId < v.getId()) {
     
    	    				TranslateAnimation animationToHide = new TranslateAnimation(0, 0, 0, -hide.getHeight());
    	    				animationToHide.setDuration(1000);
    	    				animationToHide.setAnimationListener(myAnimationListener);
     
        					// For each between the wanted description and the current showed one
        					// We will create a translation for getting up the objects
            				for (int i = (currentId + 1) ; i < (v.getId() + 1) ; i ++ ) {
     
            					TextView currentTv = (TextView)layout.findViewById(i);
        	    				currentTv.setAnimation(animationToHide);
            				}
     
        				// Else, we will getting down every object between
        				} else {
     
    	    				TranslateAnimation animationToHide = new TranslateAnimation(0, 0, 0, hide.getHeight());
    	    				animationToHide.setDuration(1000);
    	    				animationToHide.setAnimationListener(myAnimationListener);
     
        					// For each between the wanted description and the current showed one
        					// We will create a translation for getting down the objects
    	    				for (int i = (v.getId() + 2) ; i < (currentId + 1) ; i ++ ) {
     
            					TextView currentTv = (TextView)layout.findViewById(i);
     
        	    				// If the one here is text view to hide, we set the 2 animations of it
        	    				if (currentTv.getId() == hide.getId()) {
        	    					AnimationSet set = new AnimationSet(false);
        	    					set.addAnimation(hiding);
        	    					set.addAnimation(animationToHide);
        	    					hide.setAnimation(set);
        	    				} else {
        	    					currentTv.setAnimation(animationToHide);
        	    				}
            				}
        				}
     
        				show = (TextView)layout.findViewById(v.getId() + 1);
        				currentId = v.getId() + 1;
        			}
        		});            
     
                layout.addView(tvTitle);
     
                // Description
            	TextView tvDesc = new TextView(activity);
            	tvDesc.setId(id ++);
            	tvDesc.setText( currentElement.get( "description" ) );
     
            	// If it is the, we show it to the user 
                if (first) {
                	tvDesc.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 5f) );
                	currentId = tvDesc.getId();
                	first = false;
                } else {
                	tvDesc.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 0f) );
                }
     
                layout.addView(tvDesc);
            }
        }
     
    	// Animation listener, it permits to reorganize all fields after the animation of accordion
        AnimationListener myAnimationListener = new AnimationListener() {
     
            public void onAnimationEnd(Animation arg0) {
     
            	// We hide the wanted view and show the other
                show.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 5f) );
                hide.setLayoutParams( new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 0, 0f) );
     
                // Alpha animation for showing the new view
    			AlphaAnimation showing = new AlphaAnimation(0, 1);
    			showing.setDuration(500);
    			show.setAnimation(showing);
            }
     
            public void onAnimationRepeat(Animation arg0) { }
     
            public void onAnimationStart(Animation arg0) { }
        };
    }

  2. #2
    Membre à l'essai
    Inscrit en
    Novembre 2006
    Messages
    19
    Détails du profil
    Informations forums :
    Inscription : Novembre 2006
    Messages : 19
    Points : 18
    Points
    18
    Par défaut
    Bonjour,

    Merci pour ce code.
    Auriez-vous un extrait de l'appel à cette méthode ?
    Quel type d'Activity ? Et l'utilisation dans le layouy ?

    A bientôt

    xoub'

Discussions similaires

  1. Classe pour la création d'un graphe xy
    Par Bob dans le forum MFC
    Réponses: 24
    Dernier message: 03/12/2009, 17h20
  2. Création dynamique de TImages
    Par Dric dans le forum C++Builder
    Réponses: 10
    Dernier message: 08/07/2002, 12h36
  3. [Kylix] Création d'un fichier lien
    Par DrQ dans le forum EDI
    Réponses: 2
    Dernier message: 14/05/2002, 21h30
  4. Création image BMP
    Par Anonymous dans le forum C
    Réponses: 2
    Dernier message: 25/04/2002, 16h04

Partager

Partager
  • Envoyer la discussion sur Viadeo
  • Envoyer la discussion sur Twitter
  • Envoyer la discussion sur Google
  • Envoyer la discussion sur Facebook
  • Envoyer la discussion sur Digg
  • Envoyer la discussion sur Delicious
  • Envoyer la discussion sur MySpace
  • Envoyer la discussion sur Yahoo