所以最初我正在按照本教程创建自定义导航抽屉:http://www.tutecentral.com/android-custom-navigation-drawer/。
但是由于我想使用BaseActivity,我根据这篇SO帖子对其进行了修改:creating base activity with navigation drawer in android。
但是,当我单击应触发抽屉的按钮时,日志会显示活动:
[0,Home]
和
com.android.internal.view.menu.ActionMenuItem@1796f4d4
但是没有抽屉打开,也没有错误。
<!-- activity_base.xml -->
<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">
<FrameLayout android:id="@+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ListView android:id="@+id/left_drawer"
android:layout_width="240dp"
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>
(我猜)我的BaseActivity.java的相关部分:
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_base);
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setDisplayShowHomeEnabled(false);
getActionBar().setHomeButtonEnabled(true);
if (savedInstanceState == null) {
displayView(0);
}
}
private class SlideMenuClickListener implements ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// display view for selected nav drawer item
displayView(position);
}
}
@Override
public void setContentView(int layoutResID) {
super.setContentView(layoutResID);
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
mContentFrame = (FrameLayout) mDrawerLayout.findViewById(R.id.content_frame);
getLayoutInflater().inflate(layoutResID, mContentFrame, true);
mTitle = mDrawerTitle = getTitle();
navMenuTitles = getResources().getStringArray(R.array.nav_drawer_items);
navMenuIcons = getResources().obtainTypedArray(R.array.nav_drawer_icons);
navDrawerItems = new ArrayList<NavDrawerItem>();
navDrawerItems.add(new NavDrawerItem(navMenuTitles[0], navMenuIcons.getResourceId(0, -1)));
navMenuIcons.recycle();
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
System.out.println("CLOSED");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(mDrawerTitle);
System.out.println("OPENED");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
public void onDrawerStateChanged(int newState) {
System.out.println("STATUS: "+newState);
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
adapter = new NavDrawerListAdapter(getApplicationContext(), navDrawerItems);
mDrawerList.setAdapter(adapter);
mDrawerList.setOnItemClickListener(new SlideMenuClickListener());
}
答案 0 :(得分:0)
所以,深入研究https://developer.android.com/training/implementing-navigation/nav-drawer.html的代码,结果我应该使用
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
取代
mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);
我不确定Inflater的位置,但至少它现在有效。