MaterialDrawer学习笔记

本文介绍了MaterialDrawer,一个多功能的Android侧滑抽屉库。通过添加依赖并直接在代码中实现,可以轻松创建抽屉效果。查看GitHub传送门以获取更多详细信息。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

传送门:https://github.com/mikepenz/MaterialDrawer
MaterialDrawer是一个多功能的侧拉抽屉第三方库
首先先添加依赖

  implementation ('com.mikepenz:materialdrawer:5.9.0@aar') {
        transitive = true
    }

MaterialDrawer的使用不需要在xml文件中添加控件,所以,直接在代码中添加代码

	 //创建登录用户对象
        IProfile iProfile = new ProfileDrawerItem()
                .withName("测试员一号")
                .withEmail("101762562@qq.com")
                .withIcon(R.drawable.picture1)
                .withIdentifier(100); //设置标识码
        //创建抽屉头部,也是代码实现
        AccountHeader header = new AccountHeaderBuilder()
                .withActivity(MainActivity.this)    //绑定显示的活动对象,如果是fragment的话就传getActivity
                .withTranslucentStatusBar(true)     //设置启用沉浸式状态栏
                .withHeaderBackground(R.color.colorPrimary)     //设置背景图片
                .addProfiles(iProfile)          //添加用户对象,可以添加多个,用逗号隔开
                .withOnAccountHeaderListener(new AccountHeader.OnAccountHeaderListener() {
                    @Override
                    public boolean onProfileChanged(View view, IProfile profile, boolean current) {
                        switch ((int) profile.getIdentifier()){
                            case 100:
                                Toast.makeText(MainActivity.this,"CLICK HEAD",Toast.LENGTH_SHORT).show();
                                break;
                        }
                        return true;
                    }
                })
                .withSavedInstance(savedInstanceState)  //将意外被杀死的Activity的状态设置回来
                .build();
        //将头部信息添加进来到抽屉
        new DrawerBuilder()
                .withActivity(MainActivity.this)
                .withAccountHeader(header)      //添加头部
                .addDrawerItems(                //添加menu菜单
                        new PrimaryDrawerItem()
                        .withName("消息")                         //主标题
                        .withDescription("啦啦啦,有人敲门啦")   //副标题
                        .withIcon(R.drawable.picture2)          //设置icon
                        .withIdentifier(1)                      //设置标识码
                        .withSelectable(false),                 //是否被选择状态
                        new SectionDrawerItem().withName("menu组"),  //分组item,类似于group标签,无点击效果
                        new ExpandableBadgeDrawerItem()     //伸缩式item
                        .withName("伸缩式圆角item")      //主标题
                        .withIcon(R.drawable.picture2)  //添加icon
                        .withIdentifier(2)              //标识码
                        .withBadge("10")               //设置圆角气泡里的数字
                        .withSubItems(
                                new SecondaryDrawerItem().withName("内部item").withIdentifier(3)  //添加子item
                        ),
                        new SwitchDrawerItem()  //添加带有switch开关的item
                        .withName("开关item1")		
                        .withIcon(R.drawable.picture2)
                        .withIdentifier(3)
                        .withCheckable(false)
 			             .withOnCheckedChangeListener(new OnCheckedChangeListener() {	//开关按钮的监听
                            @Override
                            public void onCheckedChanged(IDrawerItem drawerItem, CompoundButton buttonView, boolean isChecked) {
                                
                            }
                        })
                )
                .withOnDrawerItemClickListener(new Drawer.OnDrawerItemClickListener() {	// item的监听
                    @Override
                    public boolean onItemClick(View view, int position, IDrawerItem drawerItem) {
                     switch ((int) draweritem.getIdentifier()){
                            case 1:
                                Toast.makeText(MainActivity.this,"CLICK 1",Toast.LENGTH_SHORT).show();
                                break;
                            case 2:
                                Toast.makeText(MainActivity.this,"CLICK 2",Toast.LENGTH_SHORT).show();
                                break;
                        }
  
                       return false;
                    }
                })
                .withSavedInstance(savedInstanceState)
                .withShowDrawerOnFirstLaunch(true)      //设置为默认启动抽屉菜单
                .build();

效果图:
在这里插入图片描述

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值