单击汉堡包图标导航抽屉上未打开 [英] on clicking hamburger icon navigation drawer not opening

查看:97
本文介绍了单击汉堡包图标导航抽屉上未打开的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

navigationDrawer不能在单击汉堡图标时打开,我已经使用过onOptionsItemSelected,但没有用:

The navigationDrawer is not opening on clicking hamburger icon,I have used onOptionsItemSelected but it's of no use:

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>

推荐答案

尝试一下

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);
            }
        };

这篇关于单击汉堡包图标导航抽屉上未打开的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆