安卓侧滑菜单简单实现

1.首先要在project structure的dependence中导入v4.design包,导入后可以在build.grade下看到如下信息:

compile 'com.android.support:design:25.2.0'
2.布局设置 ,这里注意navigationView的根布局一定是drawerLayout
    android:id="@+id/drawerLayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

            android:layout_width="match_parent"
        android:layout_height="match_parent">

                    android:id="@+id/xrv_hot_job"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_marginTop="5dp"
            android:background="@color/white" />

                            android:id="@+id/xrv_spread"
                android:background="#00000000"
                android:visibility="gone"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:layout_marginTop="5dp" />

    

            xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/navigation_view"
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:layout_gravity="right"
        android:fitsSystemWindows="true"
        app:headerLayout="@layout/layout_select_content"
        />

3.android.support.design.widget.NavigationView 的使用

  xmlns:app="http://schemas.android.com/apk/res-auto"
        app:headerLayout="@layout/head"
        app:menu="@menu/new_menu"
重写的hearderLayout既是侧滑的头布局

接下来是菜单menu:

xml version="1.0" encoding="UTF-8" ?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
    <group
        android:checkableBehavior="single">
        <item
            android:id="@+id/favorite"
            android:icon="@mipmap/v"
            android:checkable="true"
            android:title="    会员"/>

    group>
    <item
        android:id="@+id/wallet"
        android:icon="@mipmap/wa"

        android:title="    钱包"/>
    <item
        android:id="@+id/photo"
        android:icon="@mipmap/photo"
        android:title="    相册"/>
    <item
        android:id="@+id/dress"
        android:icon="@mipmap/dress1"
        android:title="    装扮"/>
    <item
        android:id="@+id/file"
        android:icon="@mipmap/file"
        android:title="    文件"/>

menu>

3.设置交互点击事件:

头布局控件一定先找到头布局,再通过头布局找各个看控件

View headerView = navigationView.getHeaderView(0);//获取头布局
 
  
public class MainActivity extends Activity implements View.OnClickListener {
    private DrawerLayout drawerLayout;
    private SystemBarTintManager tintManager;
    private NavigationView navigationView;
    ImageView menu;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        requestWindowFeature(Window.FEATURE_NO_TITLE);
        setContentView(R.layout.activity_main);
        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) {
                //item.setChecked(true);
                Toast.makeText(MainActivity.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);
        }
    }
}

4.侧滑菜单不仅通过手指滑动来实现,还可以通过主页面的菜单键点击的实现,详情看上面代码,可以为菜单项设置点击事件,通过
setNavigationItemSelectedListener

NavigationView的确没有相关关闭侧滑手势的属性,但是别忘了,drawerLayout却可以,通过以下属性可以关闭滑动手势操作:

在android抽屉开发中,需要关闭手势滑动,来滑出弹出框

mDrawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); //关闭手势滑动
mDrawer_layout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); //打开手势滑动


5.侧滑滑动事件监听 通过drawerLayout的addDrawerListener事件即可实现

mBindingView.drawerLayout.addDrawerListener(new DrawerLayout.DrawerListener() {
    @Override
    public void onDrawerSlide(View drawerView, float slideOffset) {
        LogUtils.e("侧拉菜单打开了");
       
    }

    @Override
    public void onDrawerOpened(View drawerView) {
      
        LogUtils.e("侧拉菜单关闭了");
    }

    @Override
    public void onDrawerClosed(View drawerView) {

    }

    @Override
    public void onDrawerStateChanged(int newState) {

    }
});
参考链接:https://blog.csdn.net/s1674521/article/details/62220187

你可能感兴趣的:(安卓开发)