关于布局:
1.使用DrawerLayout控件前需添加依赖com.android.support:design:24.2.1
2.由<android.support.v4.widget.DrawerLayout/>控件包裹整体布局
3.侧拉栏布局由<android.support.design.widget.NavigationView/>包裹
注意事项:
1)此布局需放在主布局下方
2)指定滑动位置 例:android:layout_gravity="left"
3)添加侧滑布局中的头部布局 app:headerLayout="@layout/header"
4)添加侧滑布局中的菜单条目 app:menu="@menu/menuview"
5)菜单条目中用<group android:checkableBehavior="single"></group>包裹
4.结合toolbar内容,创建主布局
关于Activity:
1)初始化DrawerLayout控件
2)开启侧滑 参数为滑动方向
activityMain.openDrawer(Gravity.LEFT);
3)判断侧滑栏状态
boolean drawerOpen = activityMain.isDrawerOpen(Gravity.LEFT);
if(drawerOpen){
Toast.makeText(this,"菜单栏关闭了",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this,"菜单栏打开了",Toast.LENGTH_SHORT).show();
}
4)侧滑的监听 activityMain.addDrawerListener(new DrawerLayout.DrawerListener() {
①当滑动时的监听
public void onDrawerSlide(View drawerView, float slideOffset) {
Toast.makeText(MainActivity.this,"侧滑正在改变中",Toast.LENGTH_SHORT).show();
}
②完全打开侧滑的监听
public void onDrawerOpened(View drawerView) {
Toast.makeText(MainActivity.this,"已完全打开侧滑",Toast.LENGTH_SHORT).show();
}
③完全关闭侧滑的监听
public void onDrawerClosed(View drawerView) {
Toast.makeText(MainActivity.this,"已完全关闭侧滑",Toast.LENGTH_SHORT).show();
}
④当状态发生改变地的监听
public void onDrawerStateChanged(int newState) {
if(newState==DrawerLayout.STATE_IDLE){//空闲状态
Toast.makeText(MainActivity.this,"空闲状态",Toast.LENGTH_SHORT).show();
}else if(newState==DrawerLayout.STATE_DRAGGING){//拖动状态
Toast.makeText(MainActivity.this,"拖动状态",Toast.LENGTH_SHORT).show();
}else if(newState==DrawerLayout.STATE_SETTLING) {//固定状态
Toast.makeText(MainActivity.this,"固定状态",Toast.LENGTH_SHORT).show();
}
}
});
5)侧滑menu的监听
navigationView.setNavigationItemSelectedListener(newNavigationView.OnNavigationItemSelectedListener() {
6)点击菜单元素退出侧滑菜单栏(可在监听事件中编写)
drawerLayout.closeDrawer(Gravity.LEFT);
7)点击某条目改为选中状态
item.setChecked(true);