简体   繁体   English

导航抽屉无法打开

[英]android - Navigation drawer not opening

So originally I was following this tutorial on creating a custom navigation drawer: http://www.tutecentral.com/android-custom-navigation-drawer/ . 因此,最初我是按照本教程创建自定义导航抽屉的: http : //www.tutecentral.com/android-custom-navigation-drawer/

But since I want to use a BaseActivity I modified it according to this SO post: creating base activity with navigation drawer in android . 但是由于我想使用BaseActivity,所以我根据这篇SO帖子对其进行了修改: 在android中使用导航抽屉创建基本活动

However, when I click the button that should trigger the drawer the logs show activity: 但是,当我单击应该触发抽屉的按钮时,日志显示活动:

[0,Home]

and

com.android.internal.view.menu.ActionMenuItem@1796f4d4

But no drawer is being opened and no error is thrown. 但是没有打开抽屉,也没有抛出错误。

<!-- 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>

The (I guess) relevant pieces of my BaseActivity.java: 我的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());
}

So, diving into the code from https://developer.android.com/training/implementing-navigation/nav-drawer.html it turns out I should have used 因此,从https://developer.android.com/training/implementing-navigation/nav-drawer.html深入研究代码,事实证明我应该使用

mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

In stead of 代替

mDrawerLayout = (DrawerLayout) getLayoutInflater().inflate(R.layout.activity_base, null);

I'm not sure where the Inflater came in, but at least it works now. 我不确定充气机的位置,但至少现在可以使用。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM