[英]Android: Navigation drawer on multiple activities
I'm trying to implement a navigation drawer on multiple activities by creating a DrawerActivity that all my other Activities inherit from. 我正在尝试通过创建我所有其他活动继承的DrawerActivity来在多个活动上实现导航抽屉。
I have used the following 2 links to get me to where I am: 我使用了以下2个链接让我到达我所在的位置:
https://developer.android.com/training/implementing-navigation/nav-drawer.html https://developer.android.com/training/implementing-navigation/nav-drawer.html
sliding drawer appear in all activities 滑动抽屉出现在所有活动中
I can see the navigation drawer (grab handle), however I cannot open it from the clicking the title in the action bar nor can I see any items in the drawer. 我可以看到导航抽屉(抓握手柄),但是我无法通过单击操作栏中的标题打开它,也无法看到抽屉中的任何项目。 I can, however, open it from sliding across. 但是,我可以通过滑动来打开它。
DrawerActivity.java DrawerActivity.java
public class DrawerActivity extends Activity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private String[] navOptions = new String[] {"Collect Offers",
"Exclusive", "Glove Box", "Servicing", "Dealer", "Settings"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drawer);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.nav_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(
this, R.layout.drawer_list_item, navOptions));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setContentView(final int layoutResID) {
DrawerLayout fullLayout = (DrawerLayout) getLayoutInflater()
.inflate(R.layout.activity_drawer, null);
LinearLayout actContent = (LinearLayout) fullLayout
.findViewById(R.id.content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
}
}
activity_drawer.xml activity_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/content"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@drawable/infiniti_splash_2x">
</LinearLayout>
<ListView android:id="@+id/nav_drawer"
android:layout_width="@dimen/nav_drawer_width"
android:layout_height="match_parent"
android:layout_gravity="start"
android:choiceMode="singleChoice"
android:divider="@android:color/transparent"
android:dividerHeight="0dp"
android:background="#111"/>
</android.support.v4.widget.DrawerLayout>
HomeActivity.java HomeActivity.java
public class HomeActivity extends DrawerActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
return super.onOptionsItemSelected(item);
}
}
activity_home.xml activity_home.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
Figured out my issue. 想出了我的问题。 The problem was that by overriding setContentView it was called a second time (from HomeActivity OnCreate()) AFTER the ActionBarDrawerToggle had already been set. 问题是,通过重写setContentView,它已经在ActionBarDrawerToggle设置之后第二次调用(来自HomeActivity OnCreate())。 This means that I was basically overwriting all the work done in the DrawerActivity OnCreate(). 这意味着我基本上覆盖了DrawerActivity OnCreate()中完成的所有工作。
Here is my modified code in the DrawerActivity 这是我在DrawerActivity中修改过的代码
public class DrawerActivity extends Activity {
protected ActionBarDrawerToggle mDrawerToggle;
private String[] navOptions = new String[] {"Collect Offers", "Exclusive", "Glove Box",
"Servicing", "Dealer", "Settings"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_drawer); NO LONGER REQUIRED
}
@Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
@Override
public void setContentView(final int layoutResID) {
DrawerLayout fullLayout= (DrawerLayout) getLayoutInflater()
.inflate(R.layout.activity_drawer, null);
LinearLayout actContent= (LinearLayout) fullLayout.findViewById(R.id.content);
DrawerLayout mDrawerLayout = (DrawerLayout) fullLayout.findViewById(R.id.drawer_layout);
ListView mDrawerList = (ListView) fullLayout.findViewById(R.id.nav_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, navOptions));
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getActionBar().setTitle(R.string.app_name);
}
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getActionBar().setTitle(R.string.app_name);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.