问题
I am trying out the navigation drawer (slide menu) given in this tutorial.
The difference with above link and mine is that instead of calling fragments I am trying to call the activity. When the app opens I am not able to see the Navigation drawer menu I can see only the action bar with HOME activity opened.
Here is the code that I changed: (Is it necessary to have a fragment or can I use activity for my first screen in Navigation Drawer?)
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(1, -1)));
navDrawerItems.add(new NavDrawerItem(navMenuTitles[1], navMenuIcons.getResourceId(2, -1),true, "200"));
navMenuIcons.recycle();
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
R.drawable.drawer,
R.string.drawer_open,
R.string.drawer_close
)
{
public void onDrawerClosed(View view)
{
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView)
{
getActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null)
{
displayView(0);
}
}
private class SlideMenuClickListener implements
ListView.OnItemClickListener
{
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
displayView(position);
}
}
private void displayView(int position)
{
switch (position)
{
case 0:
//fragment = new HomeFragment();
Intent intent = new Intent(this, Home.class);
startActivity(intent);
return;
case 1:
//fragment = new FindPeopleFragment();
Intent intent1 = new Intent(this, Profile.class);
startActivity(intent1);
break;
case 2:
//fragment = new PhotosFragment();
Intent intent2 = new Intent(this, Details.class);
startActivity(intent2);
break;
default:
break;
}
mDrawerList.setItemChecked(position, true);
mDrawerList.setSelection(position);
setTitle(navMenuTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
How do I fix this to show Navigation drawer on my Home Activity?
Update:
I even tried the following option given in this link:
How can I call one of my activity using navigation drawer ? but I am still not getting the navigation slide menu.
回答1:
You can't do that... Navigation drawer is a layout from an activity and you cant show an activity inside another activity, you need to use a fragments for this!
An activity is a screen, you cant show a screen inside another screen, but a fragment may be a component of a screen, and you can inflate a fragment inside a container of an activity and then show to the user.
if you wan't do this, you can create a abstract activity and inherit, but you will not have a a activity in a fragment, you will have a multiple activities with each one with your own navigation drawer.
回答2:
If you look at the sample documentation for Android Navigation drawer, it is explicitely defined to use fragments instead of activities to load different 'pages' from the navigation bar. As the app lifecycle for Android goes, fragments are easier to load and produce less impact on the Android system. they can also be easily set into the background and be replaced by other different fragments.
Having said that, your best approach would be to convert your activities into fragments and use them to load the different pages you need in your app. It is not such a difficult task, and I will show you how:
Change all activities to fragments
public class nameOfFragment extends Fragment { }
Use the onCreateView method instead of onCreate
public View onCreateView() { View view = inflater.inflate(R.layout.activity_main, container, false); //any other elements in that view need to be included here in this manner. Button rate = (Button) rootView.findViewById(R.id.rate); return view; }
These changes should be enough to change your activities into fragments. You need to do the same for all fragments which are accessed through your navigation bar.
Please post your code for the activities you are using if you need any further help. Hope this helps :)
回答3:
You've got the code to build and display a navigation bar right? Put your code above into a base activity class. Every activity that you want to access the navigation bar from should then inherit from that base class.
Google's IO 2014 app does the same thing, take a look at their source here.
When you do this, you will need to find a way to maintain the drawer's state as you transition to the next activity. I'm not sure how to do that, but you'll probably find the answer in the source code for the IO app.
回答4:
If you have to have single instance of drawer, then you have to go by fragments.
If you have no choice to go other than with activities, define a base class for all your activites(say BaseActivity). And in BaseActivity - you can do the necessary coding to integrate drawer. Each activity extending BaseActivity will now show up drawer menu (but the instances will be different)
回答5:
Now you need to use the toolbar. Android has made appcompat library to introduce material design in older versions of android as well. You need to use compile dependency of appcompat. Change the style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<!-- Customize your theme here. -->
</style>
</resources>
Then we will choose to have no old action bar. After then we make a contract between navigation drawer and toolbar. So we need not to use old actionbar now. You can refer the code below:
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff6d7fe2"
app:contentInsetEnd="0dp"
app:contentInsetStart="0dp"
></android.support.v7.widget.Toolbar>
来源:https://stackoverflow.com/questions/25670350/navigation-drawer-and-with-activity-in-android