Navigation Drawer ActionBar button not working

后端 未结 4 983
盖世英雄少女心
盖世英雄少女心 2020-12-07 20:48

I am working on an android project and I\'m trying to integrate the new Navigation Drawer using the example from http://developer.android.com/training/implementing-navigatio

相关标签:
4条回答
  • 2020-12-07 20:57

    You forgot to implement onOptionsItemSelected

    This is where the magic happens:

    @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Pass the event to ActionBarDrawerToggle, if it returns
            // true, then it has handled the app icon touch event
            if (mDrawerToggle.onOptionsItemSelected(item)) {
              return true;
            }
            // Handle your other action bar items...
    
            return super.onOptionsItemSelected(item);
        }
    
    0 讨论(0)
  • 2020-12-07 21:00

    Here i will tell you the SIMPLE AND EASY way to create drawer navigation in android without using of android studio.I have just used an ADT to create navigation drawer. Here is the code take a look

    activity_main.xml

    <android.support.v4.widget.DrawerLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/dr_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    
        <RelativeLayout 
            android:id="@+id/mainContent"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content">
         <TextView 
            android:id="@+id/txt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="swipe content"/>
         <Button 
            android:id="@+id/bt1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text = "Click to open d"/>
       </RelativeLayout>
       <RelativeLayout 
            android:id="@+id/drawer"
            android:layout_width="200dp"
            android:layout_height="match_parent"
            android:layout_gravity = "start"
            android:background="#FFFFFF">
        <TextView 
             android:id="@+id/txt2"
             android:layout_width="200dp"
             android:layout_height="wrap_content"
             android:text="drawer content are here arr"/>
        <Button 
             android:id="@+id/bt2"
             android:layout_width="wrap_content"
             android:layout_height="wrap_content"
             android:text = "Click to open d"/>
       </RelativeLayout>
    
    </android.support.v4.widget.DrawerLayout>
    

    MainActivity.java

    package com.example.drawer1;
    import android.os.Bundle;
    import android.annotation.SuppressLint;
    import android.app.Activity;
    import android.content.res.Configuration;
    import android.support.v4.app.ActionBarDrawerToggle;
    import android.support.v4.widget.DrawerLayout;
    import android.support.v4.widget.DrawerLayout.DrawerListener; 
    import android.util.Log;
    import android.view.Menu;
    import android.view.MenuItem;
    import android.view.MotionEvent;
    import android.view.View;
    import android.view.View.OnClickListener;
    import android.view.View.OnTouchListener;
    import android.widget.Button;
    import android.widget.Toast;
    
    public class MainActivity extends Activity {
        private DrawerLayout drawerLayout;
        private View drawerView;
        Button bt1,bt2;
        private DrawerListener myDrawerListner;
        private ActionBarDrawerToggle mDrawerTogle;
        @SuppressLint("NewApi")
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            drawerLayout = (DrawerLayout) findViewById(R.id.dr_layout);
            drawerView = (View) findViewById(R.id.drawer);
            drawerLayout.setDrawerListener(myDrawerListner);
            bt1 = (Button) findViewById(R.id.bt1);
            bt2 = (Button) findViewById(R.id.bt2);
            mDrawerTogle =new ActionBarDrawerToggle(this, drawerLayout,  
            R.drawable.ic_drawer,R.string.open_drawer,R.string.close_drawer){
                public void onDrawerOpened(View drawerView) {
                // TODO Auto-generated method stub
                    super.onDrawerOpened(drawerView);
                    getActionBar().setTitle("SpeakEng");
                }
                public void onDrawerClosed(View view) {
                // TODO Auto-generated method stub
                    super.onDrawerClosed(view);
                    getActionBar().setTitle("SpeakEng");
                }
            };
            drawerLayout.setDrawerListener(mDrawerTogle);
    
            getActionBar().setDisplayHomeAsUpEnabled(true);
            getActionBar().setHomeButtonEnabled(true);
    
            bt2.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                       drawerLayout.closeDrawer(drawerView);
                }
            });
    
            bt1.setOnClickListener(new OnClickListener() {
    
                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                       drawerLayout.openDrawer(drawerView);
    
                }
            });
        }
        @Override
        public void onConfigurationChanged(Configuration newConfig){
            super.onConfigurationChanged(newConfig);
            mDrawerTogle.onConfigurationChanged(newConfig);
        }
        public boolean onOptionsItemSelected(MenuItem item){
            if (mDrawerTogle.onOptionsItemSelected(item)){
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
        @Override
        protected void onPostCreate(Bundle savedInstanceState){
            super.onPostCreate(savedInstanceState);
            //Sync the toogle state after onRestoreInstanceState has occured.
            mDrawerTogle.syncState();
        }
       }
    

    You can open the drawer 1)by clicking on button,2)pulling from left,3)on clicking drawer icon on action bar.as you want you can open it.I gave you three option.

    note: drawerLayout must be root element as shown in code. and keep onConfigurationChanged(),onOptionsItemSelected(),onPostCreate(). this three methods are very important to create a navigation drawer.

    Best of luck!.

    0 讨论(0)
  • 2020-12-07 21:13

    Its work for me.

    drawerToggle.setToolbarNavigationClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                mDrawer.openDrawer(GravityCompat.START);
            }
        });
    
    0 讨论(0)
  • 2020-12-07 21:20

    For those who are still having trouble, you may be missing this method (which OP has but I had deleted):

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);
        mDrawerToggle.syncState();
    }
    
    0 讨论(0)
提交回复
热议问题