我的HomeScreen.java

    public class HomeScreen extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener{
            private Toolbar toolbar;
            private DrawerLayout drawerLayout;
            private NavigationView navigationView;
            private ActionBarDrawerToggle toggle;

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

                instantiateViews();
toggle = new ActionBarDrawerToggle(this,drawerLayout,toolbar,R.string.navigation_drawer_open,R.string.navigation_drawer_close);
                drawerLayout.addDrawerListener(toggle);
                toggle.syncState();
                setSupportActionBar(toolbar);
                navigationView.setNavigationItemSelectedListener(this);

        }

            public void instantiateViews(){

                    toolbar = findViewById(R.id.drawer_menu_toolbar);
                    drawerLayout = findViewById(R.id.home_screen_main_drawer_layout);
                    navigationView = findViewById(R.id.home_screen_navigation_view);

                }

        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            if (toggle.onOptionsItemSelected(item)) {
                Log.i("CLICKED","clicked");
                drawerLayout.openDrawer(GravityCompat.START);
                return true;
            }


     return super.onOptionsItemSelected(item);
            }


我的问题:

最初单击ActionBarToggle时,导航抽屉未打开。仅当我最初向右滑动时才打开,此后,ActionBarToggle和向右滑动都打开抽屉。仅当应用程序重新启动时,对ActionBarToggle的单击才起作用。因此,我尝试注册ActionBarToggle的点击,并在日志中注册,每当我单击它时,它将显示以下内容

Log.i

2019-06-21 10:00:11.860 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:13.282 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:14.201 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.031 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:15.830 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:16.383 7467-7467/com.example.gofresh I/CLICKED: clicked
2019-06-21 10:00:17.920 7467-7467/com.example.gofresh I/chatty: uid=10184(com.example.gofresh) identical 2 lines
2019-06-21 10:00:18.497 7467-7467/com.example.gofresh I/CLICKED: clicked


我应该在onOptionsItemSelected()中放入什么代码,以便ActionBarToggle甚至响应第一次单击?

我的home_screen.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/home_screen_main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeScreen">

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

        <!--Top Layout-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_alignParentTop="true"
            android:layout_height="120dp"
            android:background="@color/blue">

            <android.support.v7.widget.Toolbar
                android:id="@+id/drawer_menu_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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


            <android.support.v7.widget.Toolbar
                android:id="@+id/search_toolbar"
                android:layout_width="match_parent"
                android:layout_height="40dp"
                android:layout_alignParentBottom="true"
                android:layout_marginStart="8dp"
                android:layout_marginEnd="8dp"
                android:layout_marginBottom="10dp"
                android:background="@drawable/seachbar_homescreen"


                <EditText
                    android:id="@+id/searchHere"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/seachbar_homescreen"
                    android:hint="Search Here"
                    android:textSize="15sp" />

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


    </RelativeLayout>

    <android.support.design.widget.NavigationView
        android:id="@+id/home_screen_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_gravity="start"
        android:visibility="gone"
        app:headerLayout="@layout/home_screen_menu_drawer_header"
        app:menu="@menu/home_screen_menu_drawer" />


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

最佳答案

它认为您缺少可见性,因为您设置了导航视图。
android:visibility =“ gone”

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.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/home_screen_main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    tools:context=".HomeScreen">

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

        <!--Top Layout-->

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="120dp"
            android:layout_alignParentTop="true"
            android:background="@color/colorPrimary">

            <android.support.v7.widget.Toolbar
                android:id="@+id/drawer_menu_toolbar"
                android:layout_width="match_parent"
                android:layout_height="?attr/actionBarSize"
                android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar">

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


                <android.support.v7.widget.Toolbar
                    android:id="@+id/search_toolbar"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:layout_alignParentBottom="true"
                    android:layout_marginStart="8dp"
                    android:layout_marginEnd="8dp"
                    android:layout_marginBottom="10dp"
                    android:background="@android:"


                <EditText
                    android:id="@+id/searchHere"
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="@drawable/seachbar_homescreen"
                    android:hint="Search Here"
                    android:textSize="15sp" />

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


    </RelativeLayout>

    <!--<android.support.design.widget.NavigationView
        android:id="@+id/home_screen_navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_alignParentStart="true"
        android:layout_gravity="start"
        android:visibility="gone"
        app:headerLayout="@layout/home_screen_menu_drawer_header"
        app:menu="@menu/home_screen_menu_drawer" />
-->
    <android.support.design.widget.NavigationView
        android:id="@+id/nav_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        app:headerLayout="@layout/home_screen_menu_drawer_header"
        app:menu="@menu/home_screen_menu_drawer" />

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


在切换问题之前处理代码运行时中的可见性

10-08 07:21