I am novice developer and I´m integrating Navigation drawer in my app with android-support v7 and I have one question. When I start the app the main layout is this:
<?xml version="1.0" encoding="utf-8"?>
<!-- The main content view --> <FrameLayout android:id="@+id/content_frame" android:layout_width="match_parent" android:layout_height="match_parent" /> <!-- The navigation drawer --> <ListView android:id="@+id/left_drawer" android:layout_width="240dp" android:layout_height="match_parent" android:layout_gravity="start" android:background="@android:color/white" android:choiceMode="singleChoice" android:divider="@android:color/transparent" android:dividerHeight="0dp" />
and this is my main activity:
drawerList.setOnItemClickListener(new OnItemClickListener() { @Override public void onItemClick(AdapterView<?> parent, View view, int position, long id) { Fragment fragment = null; switch (position) { case 0: fragment = new Fragment1(); break; case 1: fragment = new Fragment2(); break; case 2: fragment = new Fragment3(); break; case 3: fragment = new Fragment4(); break; } FragmentManager fragmentManager = getSupportFragmentManager(); fragmentManager.beginTransaction() .replace(R.id.content_frame, fragment) .commit(); drawerList.setItemChecked(position, true); tituloSeccion = opcionesMenu[position]; getSupportActionBar().setTitle(tituloSeccion); drawerLayout.closeDrawer(drawerList); } });
How can I set default fragment like main layout of the app? Thank you
The drawer icon is displayed on all top-level destinations that use a DrawerLayout . To add a navigation drawer, first declare a DrawerLayout as the root view. Inside the DrawerLayout , add a layout for the main UI content and another view that contains the contents of the navigation drawer.
Navigation drawers provide access to destinations and app functionality, such as switching accounts. They can either be permanently on-screen or controlled by a navigation menu icon. Navigation drawers are recommended for: Apps with five or more top-level destinations.
To use a DrawerLayout, position your primary content view as the first child with width and height of match_parent and no layout_gravity> . Add drawers as child views after the main content view and set the layout_gravity appropriately. Drawers commonly use match_parent for height with a fixed width.
If it is ok for you to load the default fragment every time your activity is created, you can put a FragmentTransaction
in onCreate()
Looks something like this:
@Override public void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); FragmentTransaction tx = getSupportFragmentManager().beginTransaction(); tx.replace(R.id.content_frame, new Fragment1()); tx.commit(); }
If you want a more sophisticated way of doing this (for example switching to a different fragment when you go back to the main activity), you can use an Intent
with extras determining the fragment in onCreate()
, where you just put your default fragment in the defaultValue
upon loading the extra:
int position = getIntent().getIntExtra("position", 1); switch(position){ ... }
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With