是否可以将抽屉菜单称为一行代码?

时间:2018-10-22 07:19:03

标签: java android drawer

我有一个抽屉菜单代码:

@SuppressWarnings("StatementWithEmptyBody")
    @Override
    public boolean onNavigationItemSelected(@NonNull MenuItem item) {
        final Handler handler = new Handler(Looper.getMainLooper());
        int id = item.getItemId();

        switch (id) {
            case R.id.drMain:
                Intent intentMain = new Intent(this, MainActivity.class);
                startActivity(intentMain);
                break;

            case R.id.drDiarys:
                Intent intentDiarys = new Intent(this, Diarys.class);
                startActivity(intentDiarys);
                break;

            case R.id.drFav:
                Intent intentFav = new Intent(this, Favorites.class);
                startActivity(intentFav);
                break;

            case R.id.drSendStory:
                Intent intentSendStory = new Intent(this, SendStory.class);
                startActivity(intentSendStory);
                break;

            case R.id.drTg:
                Intent intentTg = new Intent(Intent.ACTION_VIEW, Uri.parse("https://t.me/joinchat/FAPWlBHEWo2oLKXcpEeN4A"));
                startActivity(intentTg);
                break;

            case R.id.drVk:
                Intent intentVk = new Intent(Intent.ACTION_VIEW, Uri.parse("https://vk.com/unspecific"));
                startActivity(intentVk);
                break;

            case R.id.drSettings:
                Intent intentSettings = new Intent(this, Settings.class);
                startActivity(intentSettings);
                break;

            case R.id.drAbout:
                Intent intentAbout = new Intent(this, Help.class);
                startActivity(intentAbout);
                break;

            case R.id.drChat:
                Intent chat = new Intent(this, Chat.class);
                startActivity(chat);
                break;
        }
        handler.postDelayed(new Runnable() {
            @Override
            public void run() {
                drawer.closeDrawers();
            }
        }, 100);
        return true;
    }

我有10多个活动正在使用此代码。 我会将这段代码放在另一个Java类(例如Drawer.class)上,并在正确的活动中运行(例如,使用getDrawerMenu)。

有可能吗?这样做的目的是减少代码行数。

0 个答案:

没有答案