导航抽屉未正确打开

时间:2016-01-03 23:13:28

标签: android navigation-drawer android-navigation-drawer

我在工具栏中添加了一个导航抽屉,并将工具栏添加到我的HomePage活动中。抽屉似乎工作正常,但只有当我将手指滑到汉堡包图标下方时才拉出来。否则,如果我只是点击图标就没有任何反应。有人知道为什么吗?我看了下面的教程,理想情况下,当点击汉堡包图标时,抽屉应该出来:https://www.youtube.com/watch?v=Lfc1q1dFB3E

我是否缺少onClick属性?这是我的代码:

public class HomePage extends AppCompatActivity implements View.OnClickListener {

private DrawerLayout drawerLayout;
private ListView drawerList;
private ActionBarDrawerToggle drawerToggle;
String[] drawerListItems;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_home_page);

    Toolbar myToolbar = (Toolbar) findViewById(R.id.toolBarHome);
    drawerLayout = (DrawerLayout)findViewById(R.id.homePageDrawer);
    drawerList = (ListView)findViewById(R.id.homePageList);
    drawerListItems = getResources().getStringArray(R.array.activities);
    drawerList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,drawerListItems));
    drawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            switch (position) {
                case 0: {
                    Intent i = new Intent(HomePage.this, HomePage.class);
                    startActivity(i);
                    break;
                }
                case 1: {
                    Intent i = new Intent(HomePage.this, Allergy.class);
                    startActivity(i);
                    break;
                }

            }
            drawerLayout.closeDrawer(drawerList);
        }
    });
    drawerToggle = new ActionBarDrawerToggle(this,drawerLayout,myToolbar,R.string.drawer_open,R.string.drawer_close)
    {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);
            invalidateOptionsMenu();
            syncState();
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
            invalidateOptionsMenu();
            syncState();
        }
    };
    drawerLayout.setDrawerListener(drawerToggle);
    setSupportActionBar(myToolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setHomeButtonEnabled(true);
    drawerToggle.syncState();
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    drawerToggle.syncState();
}

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    drawerToggle.onConfigurationChanged(newConfig);
}

@Override
public boolean onOptionsItemSelected(MenuItem item) {
    switch (item.getItemId()){
        case R.id.home:{
            if (drawerLayout.isDrawerOpen(drawerList)){
                drawerLayout.closeDrawer(drawerList);
            }else{
                drawerLayout.openDrawer(drawerList);
            }
            return true;
        }
        default:return super.onOptionsItemSelected(item);
    }
}

这是XML布局。我的主页活动上有一个ImageView。当NavigationDrawer退出时,我需要它来覆盖现有的ImageView,所以我把它放在我的drawerlayout中的相对布局中:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:background="@drawable/boardlayered"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">


    <android.support.v7.widget.Toolbar
    android:id="@+id/toolBarHome"
    android:layout_width="match_parent"
    android:layout_height="?attr/actionBarSize"
    android:background="?attr/colorPrimary">

   </android.support.v7.widget.Toolbar>

   <android.support.v4.widget.DrawerLayout
    android:id="@+id/homePageDrawer"
    android:layout_below="@+id/toolBarHome"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <ImageView
            android:id="@+id/dogPic"
            android:background="@drawable/happy_dog"
            android:layout_width="175dp"
            android:layout_height="200dp"
            android:layout_alignParentBottom="true"
            android:layout_marginLeft="20dp" />

    </RelativeLayout>


    <ListView
        android:id="@+id/homePageList"
        android:background="@android:color/white"
        android:layout_gravity="start"
        android:layout_width="305dp"
        android:layout_height="match_parent"></ListView>

   </android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:1)

找到它。问题在于这段代码,它直接负责汉堡包图标执行操作:

public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
    case R.id.home:{
        if (drawerLayout.isDrawerOpen(drawerList)){
            drawerLayout.closeDrawer(drawerList);
        }else{
            drawerLayout.openDrawer(drawerList);
        }
        return true;
    }
    default:return super.onOptionsItemSelected(item);
}}

特别是

case R.id.home:

应该是

case android.R.id.home:

后一种情况语法是有效的

答案 1 :(得分:0)

我认为这里的问题是:

getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);

这似乎会尝试将您的切换按钮用作“向上”导航按钮,如果您尝试使用汉堡按钮打开和关闭抽屉,则可以删除这些线条。