Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Navigation drawer default fragment

Tags:

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

like image 644
user3383415 Avatar asked Mar 10 '14 09:03

user3383415


People also ask

How to set navigation drawer in android studio?

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.

What is a 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.

How to use drawer layout in android?

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.


1 Answers

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){      ...  } 
like image 123
super-qua Avatar answered Oct 09 '22 13:10

super-qua