Bonjour,
J'ai implémenté une sorte de sliding menu sans bibliothèque. Il est possible de le déplier ou de le fermer à partir d'un bouton dans la vue et d'un bouton dans l'actionBar. Les deux boutons appellent la même méthode (optionsAction()) pour provoquer l'animation. Voici mon code :
MainActivity.java :
CollapseAnimation.java :
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 public class MainActivity extends Activity { private LinearLayout menuParams; private Button btnToggleMenuList; private int screenWidth; private boolean isExpanded; private DisplayMetrics metrics; @Override protected void onCreate(Bundle pSavedInstanceState) { super.onCreate(pSavedInstanceState); setContentView(R.layout.activity_main); menuParams = (LinearLayout) findViewById(R.id.linearLayout2); btnToggleMenuList = (Button) findViewById(R.id.button1); metrics = new DisplayMetrics(); getWindowManager().getDefaultDisplay().getMetrics(metrics); screenWidth = metrics.widthPixels; //bouton appelant l'animation, marche parfaitement btnToggleMenuList.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { optionsAction(); } }); } //fonction provoquant l'animation private void optionsAction() { if (!isExpanded) { isExpanded = true; menuParams.startAnimation(new ExpandAnimation(menuParams, 0, (int) (screenWidth * 0.2), 5)); Toast.makeText(getApplicationContext(), "isExpanded : " + isExpanded, Toast.LENGTH_SHORT).show(); } else { isExpanded = false; menuParams.startAnimation(new CollapseAnimation(menuParams, 0, (int) (screenWidth * 0.2), 5)); Toast.makeText(getApplicationContext(), "isExpanded : " + isExpanded, Toast.LENGTH_SHORT).show(); } } @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.main, menu); return true; } //Listener du bouton de l'action bar. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_options: optionsAction(); break; default: break; } return true; } }
ExpandAnimation.java :
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 public class CollapseAnimation extends Animation implements Animation.AnimationListener { private LinearLayout container; private static int ANIMATION_DURATION; private static int REPEAT_COUNT; private int LastWidth; private int FromWidth; private int ToWidth; private static int STEP_SIZE=30; public CollapseAnimation(LinearLayout pContainer, int pFromWidth, int pToWidth, int pDuration) { this.container = pContainer; LayoutParams lyp = container.getLayoutParams(); ANIMATION_DURATION = 1; REPEAT_COUNT = pDuration; this.FromWidth = lyp.width; this.ToWidth = lyp.width; setDuration(ANIMATION_DURATION); setRepeatCount(REPEAT_COUNT); setFillAfter(false); setInterpolator(new AccelerateInterpolator()); setAnimationListener(this); } @Override public void onAnimationEnd(Animation animation) { LayoutParams lyp = container.getLayoutParams(); lyp.width = 0; container.setLayoutParams(lyp); LastWidth = 0; } @Override public void onAnimationRepeat(Animation animation) { LayoutParams lyp = container.getLayoutParams(); lyp.width = lyp.width - ToWidth/REPEAT_COUNT; container.setLayoutParams(lyp); } @Override public void onAnimationStart(Animation animation) { LayoutParams lyp = container.getLayoutParams(); LastWidth = lyp.width; container.setLayoutParams(lyp); } }
Et le layout de l'activité, activity_main.xml :
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 public class ExpandAnimation extends Animation implements Animation.AnimationListener { private LinearLayout container; private static int ANIMATION_DURATION; private static int REPEAT_COUNT; private int LastWidth; private int FromWidth; private int ToWidth; private static int STEP_SIZE=30; public ExpandAnimation(LinearLayout pContainer, int pFromWidth, int pToWidth, int pDuration) { this.container = pContainer; ANIMATION_DURATION = 1; REPEAT_COUNT = pDuration; this.FromWidth = pFromWidth; this.ToWidth = pToWidth; setDuration(ANIMATION_DURATION); setRepeatCount(REPEAT_COUNT); setFillAfter(false); setInterpolator(new AccelerateInterpolator()); setAnimationListener(this); } @Override public void onAnimationEnd(Animation animation) { // Nothing } @Override public void onAnimationRepeat(Animation animation) { LayoutParams lyp = container.getLayoutParams(); lyp.width = LastWidth +=ToWidth/REPEAT_COUNT; container.setLayoutParams(lyp); } @Override public void onAnimationStart(Animation animation) { LayoutParams lyp = container.getLayoutParams(); lyp.width = FromWidth; container.setLayoutParams(lyp); LastWidth = FromWidth; } }
Quand on clique sur le bouton, le menu se déroule. On reclique dessus, ils s'enroule et ainsi de suite, le bouton fonctionne normalement et appelle optionAction() qui gère les appels startAnimation avec les deux autres classes en paramètre.
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 <?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout1" android:layout_width="match_parent" android:layout_height="match_parent" > <LinearLayout android:id="@+id/linearLayout2" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.01" android:background="@color/MallowRCMS" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/linearLayout3" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_weight="0.99" android:orientation="vertical" > <TableRow android:id="@+id/tableRow1" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <LinearLayout android:id="@+id/linearLayout5" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" > " <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="2dp" android:text="ToggleMenu" /> <ListView android:id="@+id/listMenu" android:layout_width="match_parent" android:layout_height="wrap_content" > </LinearLayout> </TableRow> </LinearLayout> </LinearLayout> </FrameLayout>
Cependant, ce n'est pas le cas de l'actionButton de l'action bar. La première fois, il déroule et enroule, puis c'est fini, plus rien. Il continue d'afficher les Toast normalement, donc il appelle toujours optionAction() mais ne lance plus les animations (pourtant les toast sont après les startAnimation) ! Il marche cependant quand le menu est déjà déroulé : il le renroule mais ne peut pas le dérouler, alors que le bouton peut...
Comment est-ce possible alors qu'il fait strictement la même chose que l'autre listener ? A savoir appeler une fonction qui ne prends pas de paramètre (et qui est donc censée être indépendante de l'appelant) ?
Merci d'avance
Partager