当前位置: 首页 > 知识库问答 >
问题:

NavigationDrawer和Up导航

宿镜
2023-03-14

我实现了一个BaseActivity,它扩展了ActionBarActivity,并实现了一个NavigationDrawer。我的所有活动都继承自此基本活动。我现在想知道我是否可以实现一个导航抽屉(NavigationDrawer),并且仍然使用工具栏进行正确的向上导航,或者当我实现导航抽屉(NavigationDrawer)时,设备后退按钮应该作为向上导航按钮吗?

编辑:

使用工具栏自定义导航抽屉

public abstract class NavigationActivity extends ActionBarActivity {
    private Toolbar toolbar = null;
    private DrawerLayout drawerLayout = null;
    private ActionBarDrawerToggle drawerToggle = null;;
    private ListView listView = null;
    private CharSequence drawerTitle = null;
    private CharSequence title = null;
    private NavigationDrawerConfiguration navigationConfig = null;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        this.navigationConfig = this.getNavigationDrawerConfiguration();
        this.setContentView(this.navigationConfig.getMainLayout());

        this.toolbar = (Toolbar) this.findViewById(R.id.app_bar);
        this.drawerLayout = (DrawerLayout) this.findViewById(navigationConfig.getDrawerLayoutId());
        this.listView = (ListView) this.findViewById(navigationConfig.getLeftDrawerId());
        this.listView.setAdapter(navigationConfig.getBaseAdapter());
        this.listView.setOnItemClickListener(new DrawerItemClickListener());
        this.drawerTitle = this.getTitle();
        this.title = this.getTitle();

        this.setSupportActionBar(this.toolbar);
        this.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        this.getSupportActionBar().setHomeButtonEnabled(true);

        this.drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, this.toolbar, this.navigationConfig.getDrawerOpenDesc(), this.navigationConfig.getDrawerCloseDesc()) {
            @Override
            public void onDrawerClosed(View view) {
                getSupportActionBar().setTitle(title);
                invalidateOptionsMenu();
            }
            @Override
            public void onDrawerOpened(View drawerView) {
                getSupportActionBar().setTitle(drawerTitle);
                invalidateOptionsMenu();
            }
        };

        this.drawerLayout.setDrawerListener(this.drawerToggle);
    }

    @Override
    protected void onPostCreate(Bundle savedInstanceState) {
        super.onPostCreate(savedInstanceState);

        this.drawerToggle.syncState();
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        super.onConfigurationChanged(newConfig);

        this.drawerToggle.onConfigurationChanged(newConfig);
    }

    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen() != null) {
            boolean drawerOpen = this.drawerLayout.isDrawerOpen(listView);

            for (int item : this.navigationConfig.getActionMenuItemsToHideWhenDrawerOpen()) {
                menu.findItem(item).setVisible(!drawerOpen);
            }
        }

        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (this.drawerToggle.onOptionsItemSelected(item)) {
            return true;
        } else {
            return false;
        }
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (keyCode == KeyEvent.KEYCODE_MENU) {
            if (this.drawerLayout.isDrawerOpen(this.listView)) {
                this.drawerLayout.closeDrawer(this.listView);
            } else {
                this.drawerLayout.openDrawer(this.listView);
            }

            return true;
        }

        return super.onKeyDown(keyCode, event);
    }

    protected NavigationDrawerConfiguration getNavigationDrawerConfiguration() {

        NavigationDrawerItem[] menu = new NavigationDrawerItem[] {
//              NavigationMenuSection.create(100, "Demos"),
                NavigationMenuItem.create(100, "Item 1", "ic_measure", false, this),
                NavigationMenuItem.create(200, "Item 2", "ic_list", false, this),
//              NavigationMenuSection.create(200, "General"),
                NavigationMenuItem.create(300, "Item 3", "ic_diagrams", false, this),
                NavigationMenuItem.create(400, "Item 4", "ic_calculator", false, this),
                NavigationMenuItem.create(500, "Item 5", "ic_scanner", false, this),
                NavigationMenuItem.create(600, "Item 6", "ic_profile", false, this),
                NavigationMenuItem.create(700, "Item 7", "ic_follower", false, this),
                NavigationMenuItem.create(800, "Item 8", "ic_settings", false, this)
        };

        NavigationDrawerConfiguration navigationDrawerConfiguration = new NavigationDrawerConfiguration();
        navigationDrawerConfiguration.setMainLayout(R.layout.activity_menu_slide);
        navigationDrawerConfiguration.setDrawerLayoutId(R.id.menu_slide_layout);
        navigationDrawerConfiguration.setLeftDrawerId(R.id.menu_slide_list);
        navigationDrawerConfiguration.setNavigationItems(menu);
        navigationDrawerConfiguration.setDrawerOpenDesc(R.string.open_navigation_drawer);
        navigationDrawerConfiguration.setDrawerCloseDesc(R.string.close_navigation_drawer);
        navigationDrawerConfiguration.setBaseAdapter(new NavigationDrawerAdapter(this, R.layout.activity_menu_slide_item_row, menu));

        return navigationDrawerConfiguration;
    }

    public void selectItem(int position) {
        NavigationDrawerItem selectedItem = this.navigationConfig.getNavigationItems()[position];

        this.onNavigationItemSelected(selectedItem.getId());
        this.listView.setItemChecked(position, true);

        if (selectedItem.updateActionBarTitle()) {
            this.setTitle(selectedItem.getLabel());
        }

        if (this.drawerLayout.isDrawerOpen(this.listView)) {
            this.drawerLayout.closeDrawer(this.listView);
        }
    }

    protected void onNavigationItemSelected(int id) {
        Intent intent = null;

        switch ((int) id) {
        case 100:
            intent = new Intent(this, MeasureDataActivity.class);
            break;
        case 200:
            intent = new Intent(this, MeasureDataListActivity.class);
            break;
        case 300:
            intent = new Intent(this, DiagramsActivity.class);
            break;
        case 400:
            intent = new Intent(this, CalcActivity.class);
            break;
        case 500:
            intent = new Intent(this, RecipeActivity.class);
            break;
        case 600:
            intent = new Intent(this, ProfileActivity.class);
            break;
        case 700:
            intent = new Intent(this, FollowerActivity.class);
            break;
        case 800:
            intent = new Intent(this, SettingsActivity.class);
            break;
        }

        intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

        this.startActivity(intent);
    }

    protected int getDrawerIcon() {
        return R.drawable.ic_navigation_drawer;
    }

    protected DrawerLayout getDrawerLayout() {
        return this.drawerLayout;
    }

    protected ActionBarDrawerToggle getDrawerToggle() {
        return this.drawerToggle;
    }

    @Override
    public void setTitle(CharSequence title) {
        this.title = title;
        this.getSupportActionBar().setTitle(title);
    }

    private class DrawerItemClickListener implements ListView.OnItemClickListener {
        /**
         * 
         */
        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
            selectItem(position);
        }
    }
}

共有1个答案

卢永寿
2023-03-14

我认为在super()调用后的onCreate中,您可以这样做:

mDrawerToggle.setDrawerIndicatorEnabled(false);

如果这一点本身不起作用(或根本不起作用),请尝试以下方法:

getActionBar().setDisplayHomeAsUpEnabled(true);

点击此链接:从ICS ActionBar切换到Lollipop工具栏后缺少向上导航图标

原始海报找到了这个链接

 类似资料:
  • 我正在将我的应用程序更新为导航架构组件,我发现它在替换导航抽屉中可见的片段时存在延迟,无法顺利关闭。 直到现在,我一直在遵循这种方法: https://vikrammnit.wordpress.com/2016/03/28/facing-navigation-drawer-item-onclick-lag/ 所以我在中导航而不是在中导航以避免故障。 这是一个非常常见的问题,但它又回来了。使用导航组

  • 问题内容: 我想做 的是打开抽屉时将随同一起滑动。我目前没有使用任何第三方库,如果可能的话,我想保持这种状态。我需要的是方法的实现,例如:; 这是我当前用于创建的代码: 问题答案: 请注意:该答案最初是在Android 4.4(KitKat)仍然很新时编写的。从Android5.0开始,尤其是由于引入了,此答案不再被认为是 最新的!但是从技术角度来看,对于那些想了解Android内部运作原理的人来

  • up

    up Stop typing ../../.. endlessly. Use tab completion instead! Using up allows you to change your current directory to a parent of the current directory where the parent is specified by name or index.

  • 所以我在我的应用程序中使用Android导航,我遇到了这样的情况: 我有2个碎片A- 我遵循这个答案,得到了这个解决方案: 当我按下硬件后退按钮时,这个解决方案工作得很好,但是当我按下工具栏上的后退按钮时,回调不会被调用。请帮帮我,谢谢。 这是我的MainActive的导航设置:

  • 描述 (Description) 弹跳动画效果用于在击中后快速向上,向后或远离表面移动元素。 语法 (Syntax) @keyframes bounceInUp { 0% { opacity: 0; transform: translateY(2000px); } 60% { opacity: 1; transform: trans

  • Up Examples Example applications, APIs, and sites for Up, organized by plan: OSS – open-source edition. Pro – commercial edition. For real-world open source example applications visit the Wiki. Notes