DrawerLayout(抽屉布局)

Android DrawerLayout使用总结
DrawerLayout使用详解
完美解决DrawerLayout抽屉实现不能点击后面的控件

效果图:

GIF.gif

布局文件最外层使用DrawerLayout

只可以有两个子布局




    

        

        

        

        
    

    

        

            
        

    



NavigationView

NavigationView侧滑菜单,可以自行填充头部布局和菜单布局,还可以再添加任意布局

在需要使用的页面添加如下代码

public class HomeActivity extends BaseActivity {
    private DrawerLayout drawerLayout;
    private ViewPager viewPager;
    private BottomNavigationView bottomNavigationView;
    private NavigationView navigationView;

    private TabFragmentPagerAdapter tabFragmentPagerAdapter;
    private List fragments;
    private MenuItem menuItem;
    private TextView tvHeader;
    private MenuItem menuItem1;
    private MenuItem menuItem2;
    private MenuItem menuItem3;
    private MenuItem menuItem4;

    @Override
    public int getLayoutId() {
        return R.layout.home_activity_home;
    }

    @Override
    public void initData() {
        initFragment();
    }

    @Override
    public void initView() {
        drawerLayout = findView(R.id.drawerLayout);
        viewPager = findView(R.id.viewPager);
        bottomNavigationView = findView(R.id.bottomNavigationView);
        navigationView = findView(R.id.navigationView);
        initViewPager();
        initBottomNavigationView();
        initNavigationView();
    }

    @Override
    public void initListener() {

    }

    @Override
    public void viewsClick(View view) {

    }

    private void initFragment() {
        fragments = new ArrayList<>();
        fragments.add(new MainFragment());
        fragments.add(new ListFragment());
        fragments.add(new ThreeFragment());
        fragments.add(new StudyFragment());
    }

    private void initViewPager() {
        tabFragmentPagerAdapter = new TabFragmentPagerAdapter(getSupportFragmentManager(), fragments);
        viewPager.setAdapter(tabFragmentPagerAdapter);
        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (menuItem != null) {
                    menuItem.setChecked(false);
                } else {
                    bottomNavigationView.getMenu().getItem(0).setChecked(false);
                }
                menuItem = bottomNavigationView.getMenu().getItem(position);
                menuItem.setChecked(true);
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });
    }

    private void initBottomNavigationView() {
        BottomNavigationViewHelper.disableShiftMode(bottomNavigationView);//取消位移动画
        bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                menuItem = item;
                int i = item.getItemId();
                if (i == R.id.navigation_home) {
                    viewPager.setCurrentItem(0);
                    return true;
                } else if (i == R.id.navigation_list) {
                    viewPager.setCurrentItem(1);
                    return true;
                } else if (i == R.id.navigation_Three) {
                    viewPager.setCurrentItem(2);
                    return true;
                } else if (i == R.id.navigation_person) {
                    viewPager.setCurrentItem(3);
                    return true;
                }
                return false;
            }
        });
    }

    private void initNavigationView() {
        //登录
        tvHeader = navigationView.getHeaderView(0).findViewById(R.id.show_nav_head_text);
        menuItem1 = navigationView.getMenu().findItem(R.id.nav_item1);
        menuItem2 = navigationView.getMenu().findItem(R.id.nav_item2);
        menuItem3 = navigationView.getMenu().findItem(R.id.nav_item3);
        menuItem4 = navigationView.getMenu().findItem(R.id.nav_item4);
        //通过actionbardrawertoggle将toolbar与drawablelayout关联起来
        ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
                this, drawerLayout, R.string.home_drawer_open, R.string.home_drawer_close) {
            @Override
            public void onDrawerSlide(View drawerView, float slideOffset) {
                //可以重新侧滑方法,该方法实现侧滑动画,整个布局移动效果
                //获取mDrawerLayout中的第一个子布局,也就是布局中的RelativeLayout
                //获取抽屉的view
                View mContent = drawerLayout.getChildAt(0);
                float scale = 1 - slideOffset;
                float endScale = 0.8f + scale * 0.2f;
                float startScale = 1 - 0.3f * scale;

                //设置左边菜单滑动后的占据屏幕大小
                drawerView.setScaleX(startScale);
                drawerView.setScaleY(startScale);
                //设置菜单透明度
                drawerView.setAlpha(0.6f + 0.4f * (1 - scale));

                //设置内容界面水平和垂直方向偏转量
                //在滑动时内容界面的宽度为 屏幕宽度减去菜单界面所占宽度
                mContent.setTranslationX(drawerView.getMeasuredWidth() * (1 - scale));
                //设置内容界面操作无效(比如有button就会点击无效)
                mContent.invalidate();
                //设置右边菜单滑动后的占据屏幕大小
                mContent.setScaleX(endScale);
                mContent.setScaleY(endScale);
            }
        };

        toggle.syncState();
        drawerLayout.addDrawerListener(toggle);

        //设置图片为本身的颜色
        navigationView.setItemIconTintList(null);
        //设置item的点击事件
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                if (item == menuItem1) {

                    drawerLayout.closeDrawer(GravityCompat.START);
                } else if (item == menuItem2) {

                    drawerLayout.closeDrawer(GravityCompat.START);
                } else if (item == menuItem3) {

                    drawerLayout.closeDrawer(GravityCompat.START);
                } else if (item == menuItem4) {

                    drawerLayout.closeDrawer(GravityCompat.START);
                }
                return true;
            }
        });
        //头部设置监听
        tvHeader.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Toast.makeText(HomeActivity.this, "登录成功", Toast.LENGTH_LONG).show();
            }
        });
    }
}

返回键关闭抽屉的问题

你可能感兴趣的:(DrawerLayout(抽屉布局))