简体   繁体   English

导航抽屉在活动之间切换

[英]Navigation Drawer to switch between activities

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. 我已经尝试过switch语句,所有这些都会导致应用程序崩溃,我不知道如何获取抽屉的单独元素以便设置它们,这样如果按下它,它将转到此页面,如果其他被按下它将转到此页面等。

Here's my 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! 如果你们有能够在活动之间切换的答案,那就太棒了!

Suppose you have 5 items (from 0 index to 4), each index identifying an Activity of your project. 假设您有5个项目(从0索引到4),每个索引标识项目的活动。 You can create a method selectItem(int position) to know what drawer item has been chosen by user. 您可以创建方法selectItem(int position)以了解用户选择的抽屉项目。

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

Finally, add this method to your DrawerItemClickListener : 最后,将此方法添加到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 Fragment s, I think !!! 我认为它比使用Fragment更容易!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM