导航抽屉活动之间切换 [英] Navigation Drawer to switch between activities

查看:220
本文介绍了导航抽屉活动之间切换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我浏览过的网站了一下,我无法找到一个答案,我的问题,我试图让我的抽屉式导航至活动,而不是碎片之间切换。我曾尝试switch语句和所有做的是崩溃的应用程序,我不知道如何获得抽屉的独立元素,以便对它们进行设置,这样,如果一个是pressed,它会到这个网页而如果对方是pressed将进入此页面等等等等。

I have browsed the website for a bit and I can't find an answer to my problem, I am trying to get my Navigation Drawer to switch between activities instead of fragments. I have tried switch statements and all that does is crash the app, I don't know how to get the separate elements of the drawer in order to set them up so that if one is pressed, it will go to this page and if the other is pressed it will go to this page etc etc.

下面是我的code,

package com.example.ColeraineTown;

imports...

public class HomeScreen extends Activity {

private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // 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));

    // 2. App Icon
    drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);

    // 2.1 create ActionBarDrawerToggle
    actionBarDrawerToggle = new ActionBarDrawerToggle(
            this,                  /* host Activity */
            drawerLayout,         /* DrawerLayout object */
            R.drawable.ic_drawer,  /* nav drawer icon to replace 'Up' caret */
            R.string.drawer_open,  /* "open drawer" description */
            R.string.drawer_close  /* "close drawer" description */
    );

    // 2.2 Set actionBarDrawerToggle as the DrawerListener
    drawerLayout.setDrawerListener(actionBarDrawerToggle);

    // 2.3 enable and show "up" arrow
    getActionBar().setDisplayHomeAsUpEnabled(true);

    drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

@Override
protected void onPostCreate(Bundle savedInstanceState) {
    super.onPostCreate(savedInstanceState);
    // Sync the toggle state after onRestoreInstanceState has occurred.
    actionBarDrawerToggle.syncState();
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {

    // call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns true
    // then it has handled the app icon touch event

    if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    return super.onOptionsItemSelected(item);
}

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {

        drawerLayout.closeDrawer(drawerListView);

    }
}

}

我一直在这个整天,试图修复它,得到它的工作,但没有运气。我花了这么久才实际抽屉摆在首位的工作,将是一种耻辱,看到这一切了。

I have been at this all day, trying to fix it and get it working, but no luck. It took me so long to get the actual drawer working in the first place it would be a shame to see it all gone.

如果你们有答案能够活动之间切换,这将是伟大的!

If you guys have the answer to being able to switch between the activities, that would be great!

推荐答案

假设你有5个项目(从0指数4),各指标确定项目的一个活动。您可以创建一个方法选择信息(INT位置)来知道什么抽屉项目已经被用户选择。

Suppose you have 5 items (from 0 index to 4), each index identifying an Activity of your project. You can create a method selectItem(int position)to know what drawer item has been chosen by user.

public void selectItem(int position) {
    Intent intent = null;
    switch(position) {
        case 0:
            intent = new Intent(this, Activity_0.class);
            break;
        case 1:
            intent = new Intent(this, Activity_1.class);
            break;

        ...


        case 4: 
            intent = new Intent(this, Activity_4.class);
            break;

        default : 
            intent = new Intent(this, Activity_0.class); // Activity_0 as default
            break;
    }

    startActivity(intent);
}

最后,这种方法添加到您的 DrawerItemClickListener

private class DrawerItemClickListener implements ListView.OnItemClickListener {
    @Override
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        selectItem(position);
        drawerLayout.closeDrawer(drawerListView);

    }
}

这比使用片段这样比较方便,我想!!!

It's easier than using Fragments, I think !!!

这篇关于导航抽屉活动之间切换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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