• dans la vue [res / layout / activity-main.xml] qui définit ces éléments et leur emplacement dans la vue ;
• dans le code de l'activité [MainActivity] ;
La suppression du menu se fait également à deux endroits :
• dans la vue [res / menu / menu-main.xml] qui définit les options du menu ;
• dans le code de l'activité [MainActivity] ;
Le code de la vue la vue [res / layout / activity-main.xml] est actuellement le suivant : 1. <?xml version="1.0"encoding="utf-8"?>
2. <android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. xmlns:tools="http://schemas.android.com/tools" 4. xmlns:app="http://schemas.android.com/apk/res-auto" 5. android:id="@+id/main_content" 6. android:layout_width="match_parent" 7. android:layout_height="match_parent" 8. android:fitsSystemWindows="true" 9. tools:context=".activity.MainActivity"> 10. 11. <android.support.design.widget.AppBarLayout 12. android:id="@+id/appbar" 13. android:layout_width="match_parent" 14. android:layout_height="wrap_content" 15. android:paddingTop="@dimen/appbar_padding_top" 16. android:theme="@style/AppTheme.AppBarOverlay"> 17. 18. <android.support.v7.widget.Toolbar 19. android:id="@+id/toolbar" 20. android:layout_width="match_parent" 21. android:layout_height="?attr/actionBarSize" 22. android:background="?attr/colorPrimary" 23. app:popupTheme="@style/AppTheme.PopupOverlay" 24. app:layout_scrollFlags="scroll|enterAlways"> 25. 26. </android.support.v7.widget.Toolbar> 27. 28. <android.support.design.widget.TabLayout 29. android:id="@+id/tabs" 30. android:layout_width="match_parent" 31. android:layout_height="wrap_content"/> 32. 33. </android.support.design.widget.AppBarLayout> 34. 35. <exemples.android.architecture.MyPager 36. android:id="@+id/container" 37. android:layout_width="match_parent" 38. android:layout_height="match_parent" 39. app:layout_behavior="@string/appbar_scrolling_view_behavior"/> 40. 41. <android.support.design.widget.FloatingActionButton 42. android:id="@+id/fab" 43. android:layout_width="wrap_content" 44. android:layout_height="wrap_content" 45. android:layout_gravity="end|bottom" 46. android:layout_margin="@dimen/fab_margin" 47. android:src="@android:drawable/ic_dialog_email"/> 48. 49. </android.support.design.widget.CoordinatorLayout>
• on supprime les lignes [28-31, 41-47] ;
Le code du menu [res / menu / menu_main.xml] est actuellement le suivant : 1. <menu xmlns:android="http://schemas.android.com/apk/res/android"
2. xmlns:app="http://schemas.android.com/apk/res-auto"
3. xmlns:tools="http://schemas.android.com/tools"
4. tools:context=".activity.MainActivity"> 5. <item android:id="@+id/action_settings"
6. android:title="@string/action_settings"
7. android:orderInCategory="100"
8. app:showAsAction="never"/> 9. <item android:id="@+id/fragment1"
10. android:title="@string/fragment1"
11. android:orderInCategory="100"
12. app:showAsAction="never"/> 13. <item android:id="@+id/fragment2"
14. android:title="@string/fragment2"
15. android:orderInCategory="100"
16. app:showAsAction="never"/> 17. <item android:id="@+id/fragment3"
18. android:title="@string/fragment3"
19. android:orderInCategory="100"
20. app:showAsAction="never"/> 21. <item android:id="@+id/fragment4"
22. android:title="@string/fragment4"
23. android:orderInCategory="100"
24. app:showAsAction="never"/> 25. </menu>
• on supprimera les lignes 9-24. On laisse ainsi une option qu'on n'utilisera pas. Simplement pour avoir un exemple de
déclaration d'une option de menu qu'on pourra reproduire par copier / coller ;
Dans la classe [MainActivity] on supprime tout ce qui fait référence aux onglets, au bouton flottant, à la barre d'outils et au menu. Pour trouver ces références, le plus simple est de supprimer leur déclaration :
1. // le gestionnaire d'onglets
2. @ViewById(R.id.tabs)
3. protected TabLayout tabLayout; 4. // le bouton flottant
5. @ViewById(R.id.fab)
6. protected FloatingActionButton fab;
et de recompiler l'application. Les lignes erronées sont celles qui font référence aux éléments disparus. Supprimez alors toutes ces lignes. Par ailleurs, modifiez le gestionnaire de fragments pour qu'il ne fasse plus référence au fragment [PlaceholderFragment] que nous avons supprimé :
1. public class SectionsPagerAdapter extends FragmentPagerAdapter { 2.
3. // les fragments
4. private AbstractFragment[] fragments; 5. 6. // constructeur 7. public SectionsPagerAdapter(FragmentManager fm) { 8. // parent 9. super(fm); 10. } 11. 12. // fragment n° position 13. @Override
14. public AbstractFragment getItem(int position) { 15. // log
16. if (IS_DEBUG_ENABLED) {
17. Log.d("SectionsPagerAdapter", String.format("getItem[%s]", position)); 18. }
19. return fragments[position]; 20. }
21.
22. // rend le nombre de fragments gérés
23. @Override
24. publicint getCount() { 25. return fragments.length; 26. }
27. }
• lignes 7-10 : on a supprimé toute la génération des fragments ;
A ce stade, il ne doit plus y avoir d'erreur de compilation. Dans la classe [MainActivity], on est arrivé au code intermédiaire suivant : 1. package exemples.android.activity; 2. 3. import android.os.Bundle; 4. import android.support.v4.app.FragmentManager; 5. import android.support.v4.app.FragmentPagerAdapter; 6. import android.support.v7.app.AppCompatActivity; 7. import android.util.Log;
12. import exemples.android.architecture.Session; 13. import exemples.android.fragments.Vue1Fragment_; 14. import org.androidannotations.annotations.*; 15.
16. @EActivity(R.layout.activity_main)
17. publicclass MainActivity extends AppCompatActivity implements IMainActivity { 18.
19. // le conteneur de fragments
20. @ViewById(R.id.container) 21. protected MyPager mViewPager; 22. // la barre d'outils
23. @ViewById(R.id.toolbar) 24. protected Toolbar toolbar; 25.
26. // injection session
27. @Bean(Session.class) 28. protected Session session; 29.
30. // nombre de fragments
31. private finalint FRAGMENTS_COUNT = 5; 32. // adjacence des fragments
33. private finalint OFF_SCREEN_PAGE_LIMIT = 2; 34.
35. // mode debug
36. publicstaticfinal boolean IS_DEBUG_ENABLED = true; 37.
38. // le gestionnaire de fragments
39. private SectionsPagerAdapter mSectionsPagerAdapter; 40.
41. // constructeur
42. public MainActivity() { 43. // log
44. if (IS_DEBUG_ENABLED) {
45. Log.d("MainActivity", "constructor"); 46. }
47. } 48.
49. @AfterViews
50. protectedvoid afterViews() { 51. // log
52. if (IS_DEBUG_ENABLED) {
53. Log.d("MainActivity", "afterViews"); 54. }
55.
56. // barre d'outils - c'est là qu'est affiché le nom de l'application
57. setSupportActionBar(toolbar); 58.
59. // le gestionnaire de fragments
60. mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager()); 61.
62. // le conteneur de fragments est associé au gestionnaire de fragments
63. // ç-à-d que le fragment n° i du conteneur de fragments est le fragment n° i délivré par le gestionnaire de fragments
64. mViewPager.setAdapter(mSectionsPagerAdapter); 65.
66. // offset des fragments
67. mViewPager.setOffscreenPageLimit(OFF_SCREEN_PAGE_LIMIT); 68.
69. // on inhibe le swipe entre fragments
70. mViewPager.setSwipeEnabled(false); 71. 72. // pas de scrolling 73. mViewPager.setScrollingEnabled(false); 74. 75. // affichage Vue1 76. navigateToView(FRAGMENTS_COUNT - 1); 77. 78. } 79. 80. @AfterInject
81. protectedvoid afterInject() { 82. // log
83. if (IS_DEBUG_ENABLED) {
84. Log.d("MainActivity", "afterInject"); 85. }
86. } 87.
88. // getter session
89. public Session getSession() { 90. return session;
91. } 92.
93. @Override
94. publicvoid navigateToView(int position) { 95. // on affiche la vue position
96. if (mViewPager.getCurrentItem() != position) { 97. // affichage fragment 98. mViewPager.setCurrentItem(position); 99. } 100. } 101. 102. // le gestionnaire de fragments
103. // c'est à lui qu'on demande les fragments à afficher dans la vue principale
104. // doit définir les méthodes [getItem] et [getCount] - les autres sont facultatives
105. publicclass SectionsPagerAdapter extends FragmentPagerAdapter { 106.
107. // les fragments
108. private AbstractFragment[] fragments; 109.
110. // constructeur
112. // parent 113. super(fm); 114. } 115. 116. // fragment n° position 117. @Override
118. public AbstractFragment getItem(int position) { 119. // log
120. if (IS_DEBUG_ENABLED) {
121. Log.d("SectionsPagerAdapter", String.format("getItem[%s]", position)); 122. }
123. return fragments[position]; 124. }
125.
126. // rend le nombre de fragments gérés
127. @Override
128. publicint getCount() { 129. return fragments.length; 130. }
131. } 132. }
Il reste quelques modifications à faire :
• supprimez la ligne 31 qui n'a plus lieu d'être ;
• ligne 33 : mettez 1 comme adjacence de fragments ;
• ligne 76 : naviguez vers la vue 0. Ce sera elle qui sera affichée la première ;
• ligne 108 : initialisez le tableau avec le fragment [Vue1Fragment_] :
1. // les fragments
2. private AbstractFragment[] fragments = new AbstractFragment[]{new Vue1Fragment_()}; On n'a donc qu'un seul fragment. Exécutez l'application. Vous devez obtenir le résultat suivant :
Le bouton [Valider] doit fonctionner.