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) { }
};
} |
Partager