如何从Navigation Drawer启动活动? [英] How to launch activity from Navigation Drawer?

查看:168
本文介绍了如何从Navigation Drawer启动活动?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我在这里搜索了关于导航抽屉的配件,当我从另一个人的问题的答案中指出了一个教程。我这样做

So I've searched allot about navigation drawers here, and when I was pointed to a tutorial from an answer in another persons question. I did so.

我成功地根据自己的喜好设计了导航抽屉的风格。
但是现在我一直在不知疲倦地搜索导航栏中如何启动活动。我设法得到一些代码进入MainActivity,但是点击该项目它不启动任何东西?所有活动都在清单中定义。我决定使用Toast作为一个跟踪和错误,仍然没有运气。

I successfully managed to create and style the nav drawer to my liking. But now I've been searching tirelessly on how I can launcher activities from the navigation drawer. I've managed to get some code into the the MainActivity but upon clicking the item it does not launch anything? All activities are define in the Manifest. I decided to use Toasts as a trail and error, still no luck.

这是我的导航栏抽屉的代码,并启动活动。

here's my code for nav drawer, and launch activity.

// Drawer Activity        
// Get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);

// Get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);

// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
// Run Activity from drawer
drawerListView.setOnItemClickListener(new DrawerItemClickListener());

这是我的DrawerItemClickListener方法

And this is my DrawerItemClickListener method

private class DrawerItemClickListener implements ListView.OnItemClickListener {

    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {

    switch(position) {
      case 0:
          Intent a = new Intent(this, AppInfo.class);
              startActivity(a);
      break;
      case 1:
          Intent b = new Intent(getBaseContext(), WelcomeActivity.class);
           startActivity(b);
            }
        }
  }


推荐答案

使用 MainActivity.this 重新分配

Intent a = new Intent(MainActivity.this, AppInfo.class);
startActivity(a);

另请更改

drawerListView.setOnItemClickListener(new DrawerItemClickListener());

替换

drawerListView.setOnItemClickListener(this);

检查自定义适配器

这篇关于如何从Navigation Drawer启动活动?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆