DrawerLayout 左侧 右侧 点击 侧滑菜单

DrawerLayout 侧滑菜单

(实现:禁止手势滑动打开侧滑菜单,只能通过点击按钮打开侧滑菜单,在侧滑菜单打开的时候,可以滑动关闭菜单)

布局






核心代码
public class DrawerLayoutActivity extends Activity {
DrawerLayout dl;
Button btnShow;
RelativeLayout rlRight;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_drawerlayout_layout);
    initView();
    initData();
}

private void initView() {
    btnShow = (Button) findViewById(R.id.btn_show);
    dl = (DrawerLayout) findViewById(R.id.drawerlayout);
    rlRight = (RelativeLayout) findViewById(R.id.right);

    // 关闭手势滑动
    dl.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
    dl.addDrawerListener(new DrawerLayout.DrawerListener() {
        @Override
        public void onDrawerSlide(View drawerView, float slideOffset) {

        }

        @Override
        public void onDrawerOpened(View drawerView) {
            // 打开手势滑动
            dl.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
        }

        @Override
        public void onDrawerClosed(View drawerView) {
            // 关闭手势滑动
            dl.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
        }

        @Override
        public void onDrawerStateChanged(int newState) {

        }
    });
}

private void initData() {
    btnShow.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            // TODO 点击按钮打开侧滑菜单
            if (!dl.isDrawerOpen(rlRight)) {
                dl.openDrawer(rlRight);
            }
        }
    });
}
}
总结

你可能感兴趣的:(DrawerLayo)