Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change fragment(?) layout or activity using a navigation drawer

I'm trying to use the new Navigation Drawer. I followed the guideline and I managed to have it working with section names title changing on item selected and so on. Now I'm stuck because I have no idea how to archive what I want to do next.

When I click on an item in the drawer - home, for example -I want it to bring me to a screen where I have all the stuff I created in a particular layout for the specific screen. It's all static stuff like TextViews and images, so I don't have to do anything with them.

Should I create an activity and a layout for every item on the ListView of the drawer and (dunno how yet) start the activity in onItemSelected()? Or can I do something like create a fragment on the fly every time I click an item in the ListView and say something like

if(homeClicked){ //use home.xml as layout }
if(contactClicked){ //use contact.xml as layout }

EDIT

As a test I put into onItemClick() a method that passed in the array position clicked. As a test I just used like:

if(position==2) { // this is "contact" in the list layout
  setContentView(R.layout.contacts);
}

It does work, but obviously I lose the drawer as the contacts layout stuff is not nested in the DrawerLayout. Is there a more elegant way to achieve my goal instead of using a switch on position and using setContentView(), modifying any layout to be nested in a DrawerLayout? (I don't even know if it's possible - probably not, as there are equal IDs)

@Stefan edited this as there's not enough space in the comment reply

Thanks Stefan, the Google example is the thing that makes me more confused. I'm sure there is a very little sneaky thing about fragments that I've probably read 10k times but my brain refuses to assimilate. As far I understood, in Google's example when you click an item in the drawer, it does create a new fragment and makes this fragment replace the only thing there is besides the drawer's ListView in the MainActivity's xml layout, which is a FrameLayout that takes all the space of the screen. Then all the fragments do is display a planet ImageView so the code says to the fragment to change that image based on the ListView array position of the item click.

like image 227
kandros Avatar asked Jun 06 '13 11:06

kandros


People also ask

Can we use activity in navigation drawer?

As for how to switch between activities via the navigation drawer, you can just set up new intents within your selectItem() method: private void selectItem(int position) { // Handle Navigation Options Intent intent; switch (position) { case 0: intent = new Intent(currentActivity, NewActivity. class); intent.

How do I create a new fragment and navigation drawer?

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.


1 Answers

Oke, a lot of starter questions here combined with one that's pretty new and undefined yet.

First of all, you start a new Activity by simply doing;

this.startActivity(new Intent(this, ActivityClassName.class)); 

From any Activity (or Fragment, a bit altered to 'this' = 'this.getActivity()' )

No matter if you use Activities or Fragments, you will always have to create a class and a belonging xml view. You can build up something generic, but most of the time you don't want to do this.

About the question if you should start up a new Activity, or switch Fragments after a click on the Drawer, i'd say thats still hard to say.

In general, it is easiest and the most 'as it should' to just start a new Activity. As an Activity should equal a visible screen with possibly multiple parts (which could be Fragments).

In practice however, you see more and more Google apps (Google Music, Play store, new Gmail..) that seem to abandon this idea and use 1 Activity which only switches Fragments constantly.

Technically this sort of places the Activity in the Application role, which can bring extra difficulties. Visually however, the transitions are much smoother as, per example, the actionbar stays the same and like that is more the 'anchor' Google likes it to be.

It seems however, that they are switching Fragments in their tutorial instead of starting Activities, so perhaps you should try it that way and be on the bleeding edge of the 'new way of doing things' :)

http://developer.android.com/training/implementing-navigation/nav-drawer.html

like image 185
Stefan de Bruijn Avatar answered Sep 28 '22 11:09

Stefan de Bruijn