Android侧滑菜单实现

一.侧滑菜单的依赖

//侧滑菜单依赖
implementation 'com.android.support:design:27.1.1'
implementation 'com.readystatesoftware.systembartint:systembartint:1.0.3'

二.布局文件




    

        

            

            

        

        
    

    

三.Activity视图交互类

import android.app.Activity;
import android.graphics.Color;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.view.MenuItem;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.Toast;
import com.readystatesoftware.systembartint.SystemBarTintManager;


//侧滑菜单
public class SlidingMenuActivity extends Activity implements View.OnClickListener{
    private DrawerLayout drawerLayout;
    private SystemBarTintManager tintManager;
    private NavigationView navigationView;
    ImageView menu;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.sliding_menu_activity);
        initWindow();
        drawerLayout = (DrawerLayout) findViewById(R.id.activity_na);
        navigationView = (NavigationView) findViewById(R.id.nav);
        menu= (ImageView) findViewById(R.id.main_menu);
        View headerView = navigationView.getHeaderView(0);//获取头布局
        menu.setOnClickListener(this);
        navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
            @Override
            public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                Log.e("------------", "" + item.getTitle());
                if (item.getTitle().equals("会员")){
                    Toast.makeText(SlidingMenuActivity.this, "你点击了会员", Toast.LENGTH_SHORT).show();
                }
//                item.setChecked(true);//记录点击的项
                Toast.makeText(SlidingMenuActivity.this, item.getTitle().toString(), Toast.LENGTH_SHORT).show();
                drawerLayout.closeDrawer(navigationView);
                return true;
            }
        });
    }
    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.main_menu://点击菜单,跳出侧滑菜单
                if (drawerLayout.isDrawerOpen(navigationView)){
                    drawerLayout.closeDrawer(navigationView);
                }else{
                    drawerLayout.openDrawer(navigationView);

                }
                break;
        }
    }

    private void initWindow() {//初始化窗口属性,让状态栏和导航栏透明
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
            getWindow().addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION);
            tintManager = new SystemBarTintManager(this);
            int statusColor = Color.parseColor("#1976d2");
            tintManager.setStatusBarTintColor(statusColor);
            tintManager.setStatusBarTintEnabled(true);
        }
    }
}

四.侧滑菜单头部及主体布局

头部:head.xml



    
    
    

主体:new_menu.xml



    
        
    
    
    
    
    

 

你可能感兴趣的:(Android侧滑菜单实现)