自定义侧滑菜单

1、布局文件

    android:id="@+id/main_drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent">


<........>



            android:id="@+id/main_left_drawer_layout"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="#ffffff"
        android:orientation="vertical">

                    android:layout_width="match_parent"
            android:layout_height="160dp"
            android:background="@mipmap/menu_top_back"
            android:gravity="center"
            android:orientation="vertical">

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:src="@mipmap/warning_remind" />

                            android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginTop="@dimen/dimen_28px"
                android:text="当前预警"
                android:textColor="#FF000000"
                android:textSize="@dimen/dimen_28px" />

       


                   android:id="@+id/recycler_menu_warningList"
           android:layout_width="match_parent"
           android:layout_height="match_parent"/>


   

2、activity的代码:
// 抽屉菜单对象
private ActionBarDrawerToggle drawerbar;
private LinearLayout main_left_drawer_layout;
    private RecyclerView mRecyclerMenuWarningList;//侧滑菜单的预警列表
    private SlidingMenuWarningListAdapter mMenuListAdapter;//预警列表适配器
    private List mWarningList = new ArrayList<>();


 /**
     * 初始化侧边菜单栏
     *
     * @author liu
     * @date 2019/9/3
     */
    private void initmenu() {
        drawerLayout = (DrawerLayout) findViewById(R.id.main_drawer_layout);
        //设置菜单内容之外其他区域的背景色
        //drawerLayout.setScrimColor(Color.TRANSPARENT);
        drawerLayout.setScrimColor(Color.parseColor("#88000000"));
        //左边菜单
        main_left_drawer_layout = (LinearLayout) findViewById(R.id.main_left_drawer_layout);
        drawerbar = new ActionBarDrawerToggle(this, drawerLayout, null, R.string.apply_reason_is_null, R.string.goods_address_null) {
            //菜单打开
            @Override
            public void onDrawerOpened(View drawerView) {
                super.onDrawerOpened(drawerView);
                LogUtils.d("侧边菜单栏打开");

            }

            // 菜单关闭
            @Override
            public void onDrawerClosed(View drawerView) {
                super.onDrawerClosed(drawerView);
                LogUtils.d("侧边菜单栏关闭");

            }
        };
        drawerLayout.addDrawerListener(drawerbar);
        //getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        //getSupportActionBar().setHomeButtonEnabled(true);
        drawerbar.syncState();
        // 禁止手势滑动
        drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        //打开手势滑动
        //drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED)

        //侧滑菜单的预警列表
        mRecyclerMenuWarningList = findViewById(R.id.recycler_menu_warningList);
        mRecyclerMenuWarningList.setLayoutManager(new LinearLayoutManager(this));
        mRecyclerMenuWarningList.addItemDecoration(new SpaceItemDecoration(20, 0, 0, 0));
        mMenuListAdapter = new SlidingMenuWarningListAdapter(mContext);
        mRecyclerMenuWarningList.setAdapter(mMenuListAdapter);

    }


    //左边菜单开关事件调用
    public void openLeftLayout(View view) {
        if (drawerLayout.isDrawerOpen(main_left_drawer_layout)) {
            drawerLayout.closeDrawer(main_left_drawer_layout);
        } else {
            drawerLayout.openDrawer(main_left_drawer_layout);
        }
    }
 

你可能感兴趣的:(Android)