点击汉堡包图标导航抽屉无法打开

时间:2018-05-16 05:19:42

标签: android navigation-drawer android-toolbar

单击汉堡包图标时navigationDrawer未打开,我使用了onOptionsItemSelected,但没有用:

public class CardDashBoard extends Fragment implements View.OnClickListener {

View view;

private ArrayList<String> mtext=new ArrayList<>();
private ArrayList<String> mimage=new ArrayList<>();
ImageView image1,image2,image3,image4;
TextView text1,text2,text3,text4;

CardView cardViewMap;



DrawerLayout drawerLayout;
Toolbar toolbar;
ActionBarDrawerToggle actionBarDrawerToggle;


public CardDashBoard() {
    // Required empty public constructor
}


@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    view= inflater.inflate(R.layout.fragment_card_dash_board, container, false);

    return view;

}

@Override
public void onActivityCreated(Bundle savedInstanceState){
    super.onActivityCreated(savedInstanceState);

    image1=view.findViewById(R.id.imgvid1);
    image2=(ImageView)view.findViewById(R.id.imgvid2);
    image3=(ImageView)view.findViewById(R.id.imgvid3);
    image4=(ImageView)view.findViewById(R.id.imgvid4);

    text1=(TextView)view.findViewById(R.id.textvid1);
    text2=(TextView)view.findViewById(R.id.textvid2);
    text3=(TextView)view.findViewById(R.id.textvid3);
    text4=(TextView)view.findViewById(R.id.textvid4);

    cardViewMap=(CardView)view.findViewById(R.id.mapcard);
    cardViewMap.setOnClickListener(this);




    //color change of icon
   // mToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.colorPrimary1));


    drawerLayout=(DrawerLayout) view.findViewById(R.id.drawerlayout1);
    toolbar=(Toolbar) view.findViewById(R.id.toolbar1);



    ((AppCompatActivity)getActivity()).setSupportActionBar(toolbar);

    actionBarDrawerToggle=new ActionBarDrawerToggle(getActivity(),drawerLayout,R.string.open,R.string.close);
    drawerLayout.addDrawerListener(actionBarDrawerToggle);
    actionBarDrawerToggle.syncState();


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


    ((AppCompatActivity)getActivity()).getSupportActionBar().setTitle(null);

    actionBarDrawerToggle.getDrawerArrowDrawable().setColor(getResources().getColor(R.color.colorPrimary1));


}


@Override
public boolean onOptionsItemSelected(MenuItem item) {
    if(actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }

    return super.onOptionsItemSelected(item);
}



/*@Override
public boolean onOptionsItemSelected(MenuItem item) {

    if (actionBarDrawerToggle.onOptionsItemSelected(item))
    {
        return true;
    }
    return super.onOptionsItemSelected(item);
}*/


}

}

这是我的xml代码

 //xml
   <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:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawerlayout1">
      <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:orientation="vertical">


        <LinearLayout
            android:id="@+id/toolbar_container1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="horizontal">


            <android.support.v7.widget.Toolbar
                android:id="@+id/tool_bar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@drawable/logincolor"
                app:popupTheme="@style/ThemeOverlay.AppCompat.Light">


            </android.support.v7.widget.Toolbar>
        </LinearLayout>
        <android.support.design.widget.NavigationView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/navi_dra1"
        android:fitsSystemWindows="true"
        app:menu="@menu/drawer_menu"
        app:itemTextColor="#6E107F"
        app:itemIconTint="#6E107F"
        android:textAlignment="center"
        android:layout_gravity="start"
        app:headerLayout="@layout/navigation_header">

       </android.support.design.widget.NavigationView>
     </LinearLayout>
  </android.support.v4.widget.DrawerLayout>

2 个答案:

答案 0 :(得分:0)

试试这个

ActionBarDrawerToggle drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.open
                , R.string.close) {
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
            }

            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
            }
        };

答案 1 :(得分:0)

尝试使用此代码为我工作

 ActionBarDrawerToggle  actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.app_name, R.string.app_name) {
        @Override
        public void onDrawerOpened(View drawerView) {
            super.onDrawerOpened(drawerView);

        }

        @Override
        public void onDrawerClosed(View drawerView) {
            super.onDrawerClosed(drawerView);
        }
    };

    drawerLayout.addDrawerListener(actionBarDrawerToggle);