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

断了今生、忘了曾经 提交于 2019-12-23 07:29:10

问题


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.


回答1:


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




回答2:


It's really confused, I am pretty sure Android engineer missed something here !

For instance on iOS, you can easily have multiple "UIViewController" on the windows and have the same menu (or UITabViewController) for all your application. Thats a normal behavior w/o 231 questions on stackoverflow ^^

But on Android, I see no one has a clean solution to use NavDrawer or Tab with Activies ;-(

I am going to chose the single activity and several fragments approach, let's see !



来源:https://stackoverflow.com/questions/16960498/change-fragment-layout-or-activity-using-a-navigation-drawer

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!