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

Composants graphiques Android Discussion :

Expandable ListeView dans menu Drawer


Sujet :

Composants graphiques Android

  1. #1
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Points : 51
    Points
    51
    Par défaut Expandable ListeView dans menu Drawer
    Bonjour à tous,
    J'aurais aimé ajouter dans un menu drawer une expandable listeview, mais voilà, je ne sais pas où commencer ni comment faire.

    Voici mes codes, si l'un de vous a une piste, une solution, un tuto...

    MainActivity
    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
    public class MainActivity extends ActionBarActivity
            implements NavigationDrawerCallbacks {
     
        /**
         * Fragment managing the behaviors, interactions and presentation of the navigation drawer.
         */
        private NavigationDrawerFragment mNavigationDrawerFragment;
        private Toolbar mToolbar;
     
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            mToolbar = (Toolbar) findViewById(R.id.toolbar_actionbar);
            setSupportActionBar(mToolbar);
     
            mNavigationDrawerFragment = (NavigationDrawerFragment)
                    getFragmentManager().findFragmentById(R.id.fragment_drawer);
     
            // Set up the drawer.
            mNavigationDrawerFragment.setup(R.id.fragment_drawer, (DrawerLayout) findViewById(R.id.drawer), mToolbar);
     
        }
     
        @Override
        public void onNavigationDrawerItemSelected(int position) {
            Fragment objFragment = null;
     
            switch (position) {
                case 0:
                    objFragment = new menu1_Fragment();
                    break;
                case 1:
                    objFragment = new menu2_Fragment();
                    break;
                case 2:
                    objFragment = new menu3_Fragment();
                    break;
                case 3:
                    objFragment = new menu4_Fragment();
                    break;
                case 4:
                    objFragment = new menu5_Fragment();
                    break;
                case 5:
                    objFragment = new menu6_Fragment();
                    break;
                case 6:
                    objFragment = new menu8_Fragment();
                    break;
                case 7:
                    objFragment = new menu9_Fragment();
                    break;
                case 8:
                    objFragment = new menu10_Fragment();
                    break;
                case 9:
                    objFragment = new menu7_Fragment();
                    break;
            }
     
            FragmentManager fragmentManager = getFragmentManager();
            fragmentManager.beginTransaction()
     
                    .replace(R.id.container, objFragment)
                    .commit();
        }
     
     
        @Override
        public void onBackPressed() {
            if (mNavigationDrawerFragment.isDrawerOpen())
                mNavigationDrawerFragment.closeDrawer();
            else
                super.onBackPressed();
        }
     
     
        @Override
        public boolean onCreateOptionsMenu(Menu menu) {
            if (!mNavigationDrawerFragment.isDrawerOpen()) {
                // Only show items in the action bar relevant to this screen
                // if the drawer is not showing. Otherwise, let the drawer
                // decide what to show in the action bar.
                getMenuInflater().inflate(R.menu.main, menu);
                return true;
            }
            return super.onCreateOptionsMenu(menu);
        }
     
    }
    NavigationDrawerFragement
    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
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
    203
    204
    205
    206
    207
    208
    209
    210
    211
    212
    213
    214
    215
    216
    217
    218
    219
    220
    221
    222
    223
    224
    225
    226
    227
    228
    229
    230
    231
    232
    233
    234
    235
    236
    237
    238
    239
    240
    241
    242
    243
    244
    245
    246
    247
    248
    249
    250
    251
    252
    253
    254
    255
    256
    257
    258
    259
    260
    261
    262
    263
    264
    265
    266
    267
    268
    269
    270
    271
    272
    273
    274
    275
    276
    277
    278
    279
    280
    281
    282
    283
    284
    285
    286
    287
    288
    289
    290
    public class NavigationDrawerFragment extends Fragment implements NavigationDrawerCallbacks {
     
        /**
         * Remember the position of the selected item.
         */
        private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
     
        /**
         * Per the design guidelines, you should show the drawer on launch until the user manually
         * expands it. This shared preference tracks this.
         */
        private static final String PREF_USER_LEARNED_DRAWER = "navigation_drawer_learned";
     
        /**
         * A pointer to the current callbacks instance (the Activity).
         */
        private NavigationDrawerCallbacks mCallbacks;
     
        /**
         * Helper component that ties the action bar to the navigation drawer.
         */
        private ActionBarDrawerToggle mActionBarDrawerToggle;
     
        private DrawerLayout mDrawerLayout;
        private RecyclerView mDrawerList;
        private View mFragmentContainerView;
     
        private int mCurrentSelectedPosition = 0;
        private boolean mFromSavedInstanceState;
        private boolean mUserLearnedDrawer;
     
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
     
            // Read in the flag indicating whether or not the user has demonstrated awareness of the
            // drawer. See PREF_USER_LEARNED_DRAWER for details.
            SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(getActivity());
            mUserLearnedDrawer = sp.getBoolean(PREF_USER_LEARNED_DRAWER, false);
     
            if (savedInstanceState != null) {
                mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
                mFromSavedInstanceState = true;
            }
        }
     
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                                 Bundle savedInstanceState) {
            View view = inflater.inflate(R.layout.fragment_navigation_drawer, container, false);
            mDrawerList = (RecyclerView) view.findViewById(R.id.drawerList);
            LinearLayoutManager layoutManager = new LinearLayoutManager(getActivity());
            layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
            mDrawerList.setLayoutManager(layoutManager);
            mDrawerList.setHasFixedSize(true);
     
            final List<NavigationItem> navigationItems = getMenu();
            NavigationDrawerAdapter adapter = new NavigationDrawerAdapter(navigationItems);
            adapter.setNavigationDrawerCallbacks(this);
            mDrawerList.setAdapter(adapter);
            selectItem(mCurrentSelectedPosition);
            return view;
        }
     
        public boolean isDrawerOpen() {
            return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
        }
     
        public ActionBarDrawerToggle getActionBarDrawerToggle() {
            return mActionBarDrawerToggle;
        }
     
        public DrawerLayout getDrawerLayout() {
            return mDrawerLayout;
        }
     
        @Override
        public void onNavigationDrawerItemSelected(int position) {
            selectItem(position);
        }
     
        public List<NavigationItem> getMenu() {
            List<NavigationItem> items = new ArrayList<NavigationItem>();
            items.add(new NavigationItem("1", getResources().getDrawable(R.drawable.1)));
            items.add(new NavigationItem("2", getResources().getDrawable(R.drawable.2)));
            items.add(new NavigationItem("3", getResources().getDrawable(R.drawable.3)));
            items.add(new NavigationItem("4", getResources().getDrawable(R.drawable.4)));
            items.add(new NavigationItem("5", getResources().getDrawable(R.drawable.5)));
            items.add(new NavigationItem("6", getResources().getDrawable(R.drawable.6)));
            items.add(new NavigationItem("7", getResources().getDrawable(R.drawable.7)));
            items.add(new NavigationItem("8", getResources().getDrawable(R.drawable.8)));
            items.add(new NavigationItem("9", getResources().getDrawable(R.drawable.9)));
            items.add(new NavigationItem("10", getResources().getDrawable(R.drawable.10)));
            return items;
        }
     
        /**
         * Users of this fragment must call this method to set up the navigation drawer interactions.
         *
         * @param fragmentId   The android:id of this fragment in its activity's layout.
         * @param drawerLayout The DrawerLayout containing this fragment's UI.
         * @param toolbar      The Toolbar of the activity.
         */
        public void setup(int fragmentId, DrawerLayout drawerLayout, Toolbar toolbar) {
            mFragmentContainerView = getActivity().findViewById(fragmentId);
            mDrawerLayout = drawerLayout;
     
            mDrawerLayout.setStatusBarBackgroundColor(getResources().getColor(R.color.myPrimaryDarkColor));
     
            mActionBarDrawerToggle = new ActionBarDrawerToggle(getActivity(), mDrawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
                @Override
                public void onDrawerClosed(View drawerView) {
                    super.onDrawerClosed(drawerView);
                    if (!isAdded()) return;
     
                    getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
                }
     
                @Override
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    if (!isAdded()) return;
                    if (!mUserLearnedDrawer) {
                        mUserLearnedDrawer = true;
                        SharedPreferences sp = PreferenceManager
                                .getDefaultSharedPreferences(getActivity());
                        sp.edit().putBoolean(PREF_USER_LEARNED_DRAWER, true).apply();
                    }
                    getActivity().invalidateOptionsMenu(); // calls onPrepareOptionsMenu()
                }
            };
     
            // If the user hasn't 'learned' about the drawer, open it to introduce them to the drawer,
            // per the navigation drawer design guidelines.
            if (!mUserLearnedDrawer && !mFromSavedInstanceState) {
                mDrawerLayout.openDrawer(mFragmentContainerView);
            }
     
            // Defer code dependent on restoration of previous instance state.
            mDrawerLayout.post(new Runnable() {
                @Override
                public void run() {
                    mActionBarDrawerToggle.syncState();
                }
            });
     
            mDrawerLayout.setDrawerListener(mActionBarDrawerToggle);
        }
     
        private void selectItem(int position) {
            mCurrentSelectedPosition = position;
            if (mDrawerLayout != null) {
                mDrawerLayout.closeDrawer(mFragmentContainerView);
            }
            if (mCallbacks != null) {
                mCallbacks.onNavigationDrawerItemSelected(position);
            }
            ((NavigationDrawerAdapter) mDrawerList.getAdapter()).selectPosition(position);
        }
     
        public void openDrawer() {
            mDrawerLayout.openDrawer(mFragmentContainerView);
        }
     
        public void closeDrawer() {
            mDrawerLayout.closeDrawer(mFragmentContainerView);
        }
     
        @Override
        public void onAttach(Activity activity) {
            super.onAttach(activity);
            try {
                mCallbacks = (NavigationDrawerCallbacks) activity;
            } catch (ClassCastException e) {
                throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
            }
        }
     
        @Override
        public void onDetach() {
            super.onDetach();
            mCallbacks = null;
        }
     
        @Override
        public void onSaveInstanceState(Bundle outState) {
            super.onSaveInstanceState(outState);
            outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
        }
     
        @Override
        public void onConfigurationChanged(Configuration newConfig) {
            super.onConfigurationChanged(newConfig);
            // Forward the new configuration the drawer toggle component.
            mActionBarDrawerToggle.onConfigurationChanged(newConfig);
        }
     
        /*public void setUserData(String user, String email, Bitmap avatar) {
            ImageView avatarContainer = (ImageView) mFragmentContainerView.findViewById(R.id.imgAvatar);
            ((TextView) mFragmentContainerView.findViewById(R.id.txtUserEmail)).setText(email);
            ((TextView) mFragmentContainerView.findViewById(R.id.txtUsername)).setText(user);
            avatarContainer.setImageDrawable(new RoundImage(avatar));
        }*/
     
        public View getGoogleDrawer() {
            return mFragmentContainerView.findViewById(R.id.googleDrawer);
        }
     
        public static class RoundImage extends Drawable {
            private final Bitmap mBitmap;
            private final Paint mPaint;
            private final RectF mRectF;
            private final int mBitmapWidth;
            private final int mBitmapHeight;
     
            public RoundImage(Bitmap bitmap) {
                mBitmap = bitmap;
                mRectF = new RectF();
                mPaint = new Paint();
                mPaint.setAntiAlias(true);
                mPaint.setDither(true);
                final BitmapShader shader = new BitmapShader(bitmap, Shader.TileMode.CLAMP, Shader.TileMode.CLAMP);
                mPaint.setShader(shader);
     
                mBitmapWidth = mBitmap.getWidth();
                mBitmapHeight = mBitmap.getHeight();
            }
     
            @Override
            public void draw(Canvas canvas) {
                canvas.drawOval(mRectF, mPaint);
            }
     
            @Override
            protected void onBoundsChange(Rect bounds) {
                super.onBoundsChange(bounds);
                mRectF.set(bounds);
            }
     
            @Override
            public void setAlpha(int alpha) {
                if (mPaint.getAlpha() != alpha) {
                    mPaint.setAlpha(alpha);
                    invalidateSelf();
                }
            }
     
            @Override
            public void setColorFilter(ColorFilter cf) {
                mPaint.setColorFilter(cf);
            }
     
            @Override
            public int getOpacity() {
                return PixelFormat.TRANSLUCENT;
            }
     
            @Override
            public int getIntrinsicWidth() {
                return mBitmapWidth;
            }
     
            @Override
            public int getIntrinsicHeight() {
                return mBitmapHeight;
            }
     
            public void setAntiAlias(boolean aa) {
                mPaint.setAntiAlias(aa);
                invalidateSelf();
            }
     
            @Override
            public void setFilterBitmap(boolean filter) {
                mPaint.setFilterBitmap(filter);
                invalidateSelf();
            }
     
            @Override
            public void setDither(boolean dither) {
                mPaint.setDither(dither);
                invalidateSelf();
            }
     
            public Bitmap getBitmap() {
                return mBitmap;
            }
     
        }
    }
    activity_main
    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
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent"
        android:layout_height="match_parent">
     
        <include android:id="@+id/toolbar_actionbar" layout="@layout/toolbar_default"
            android:layout_width="match_parent" android:layout_height="wrap_content" />
     
        <android.support.v4.widget.DrawerLayout android:id="@+id/drawer"
            xmlns:android="http://schemas.android.com/apk/res/android"
            android:layout_width="match_parent" android:layout_height="match_parent"
            android:layout_below="@+id/toolbar_actionbar">
     
            <FrameLayout android:id="@+id/container" android:layout_width="match_parent"
                android:clickable="true" android:layout_height="match_parent" />
     
            <!-- android:layout_marginTop="?android:attr/actionBarSize"-->
            <fragment android:id="@+id/fragment_drawer"
                android:name="gisclace.foobar.NavigationDrawerFragment"
                android:layout_width="@dimen/navigation_drawer_width"
                android:layout_height="match_parent" android:layout_gravity="start"
                app:layout="@layout/fragment_navigation_drawer" />
        </android.support.v4.widget.DrawerLayout>
    </RelativeLayout>
    drawer_row
    Code : Sélectionner tout - Visualiser dans une fenêtre à part
    1
    2
    3
    4
    5
    6
    7
    8
    9
    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_height="48dp" android:layout_width="match_parent">
     
        <TextView android:id="@+id/item_name" android:layout_width="match_parent"
            android:layout_height="match_parent" android:layout_marginLeft="16dp"
            android:layout_marginStart="16dp" android:layout_marginRight="16dp"
            android:drawablePadding="16dp" android:layout_marginEnd="16dp" android:textSize="14sp"
            android:gravity="center_vertical" android:textColor="@color/myTextPrimaryColor" />
    </LinearLayout>
    Merci par avance.

  2. #2
    Membre du Club
    Profil pro
    Inscrit en
    Janvier 2007
    Messages
    83
    Détails du profil
    Informations personnelles :
    Localisation : France

    Informations forums :
    Inscription : Janvier 2007
    Messages : 83
    Points : 51
    Points
    51
    Par défaut
    Personne ?

Discussions similaires

  1. Réponses: 8
    Dernier message: 16/03/2006, 07h36
  2. [forms 9.04.0.19] Appel de form dans menu
    Par pdiaz dans le forum Forms
    Réponses: 6
    Dernier message: 06/07/2005, 08h05
  3. Icône dans menu contextuel
    Par Larion dans le forum C++Builder
    Réponses: 4
    Dernier message: 01/04/2005, 22h45
  4. [InnoSetup] Ajouter un raccourci dans menu Démarrer
    Par jlvalentin dans le forum Installation, Déploiement et Sécurité
    Réponses: 3
    Dernier message: 23/02/2005, 16h26
  5. [Systeme]Win XP pro: pb d'icone dans menu Démarrer
    Par Bountyx dans le forum Windows XP
    Réponses: 2
    Dernier message: 20/01/2005, 12h22

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