簡體   English   中英

無法通過按鈕單擊打開導航抽屜(僅通過手勢)

[英]Unable to open navigation drawer by button click (only by gesture)

我可以通過手勢顯示導航抽屜,但不能通過按鈕單擊。 用工具欄試過了,還是不行。 有任何想法嗎?

ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.open, R.string.cancel);
toggle.setDrawerIndicatorEnabled(true);
navigation.bringToFront();
drawerLayout.addDrawerListener(toggle);
navigation.setNavigationItemSelectedListener(this);
toggle.syncState();

//code to open on regular button click
if (drawerLayout.isDrawerVisible(GravityCompat.START)) {
    drawerLayout.closeDrawer(GravityCompat.START);
} else {
    drawerLayout.openDrawer(navigation);
}

此外,當通過手勢調用抽屜時,永遠不會調用 onNavigationItemSelected。 不知道為什么,一切都准備好了(我檢查了很多很多教程)。

<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:openDrawer="start">

    <FrameLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true">

        <FrameLayout
            android:id="@+id/fragmentContainer"
            android:layout_width="match_parent"
            android:layout_height="match_parent" />
    </FrameLayout>

    <com.google.android.material.navigation.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/nav_header"
        app:menu="@menu/navigation_menu" />
</androidx.drawerlayout.widget.DrawerLayout>

更新:

@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {
    Log.d(TAG, "onNavigationItemSelected:");
    if (item.getItemId() == R.id.optionServices) {
        Toast.makeText(MainActivity.this, "optionServices", Toast.LENGTH_SHORT).show();
    }
    return true;
}

嘗試使用標志,

mDrawerLayout.openDrawer(Gravity.LEFT);
mDrawerLayout.closeDrawer(Gravity.LEFT);

詳細參考這里的代碼

你會分享 setNavigationItemSelectedListener 的實現嗎? 您可能沒有正確返回 boolean 值。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM