简体   繁体   中英

How to use OnItemClickListener in Navigation Drawer?

I have a Navigation Drawer in my App.I have the items in the drawer but I don't know how I can open new Activity from each of items .

I have Tried some Methods & Commented It ...

Thanks for your Help ...

MainActivity

public class MainActivity extends ActionBarActivity
{
    private ListView mDrawerList;
    protected DrawerLayout mDrawer;
    private CustomActionBarDrawerToggle mDrawerToggle;
    private String[] menuItems;
    String LOG_TAG = "Remote It";

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
        // getSupportActionBar().hide();
        setContentView(R.layout.activity_main_drawer);

        // enable ActionBar app icon to behave as action to toggle nav drawer
        getSupportActionBar().setDisplayHomeAsUpEnabled(true);
        getSupportActionBar().setHomeButtonEnabled(true);

        mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);

        // set a custom shadow that overlays the main content when the drawer
        // opens
        mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);

        _initMenu();
        mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
        mDrawer.setDrawerListener(mDrawerToggle);
        // if (savedInstanceState == null)
        // {
        // selectItem(0);
        // }

    }

    private void _initMenu()
    {
        NsMenuAdapter mAdapter = new NsMenuAdapter(this);

        // Add Header
        mAdapter.addHeader(R.string.ns_menu_main_header);

        // Add first block

        menuItems = getResources().getStringArray(R.array.ns_menu_items);
        String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);

        int res = 0;
        for (String item : menuItems)
        {

            int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
            int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());

            NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
            // if (res==1) mItem.counter=12; //it is just an example...
            // if (res==3) mItem.counter=3; //it is just an example...
            mAdapter.addItem(mItem);
            res++;
        }

        mAdapter.addHeader(R.string.ns_menu_main_header2);

        mDrawerList = (ListView) findViewById(R.id.drawer);
        if (mDrawerList != null)
            mDrawerList.setAdapter(mAdapter);

        mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

    }

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

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

    @Override
    public boolean onCreateOptionsMenu(Menu menu)
    {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.control_menu, menu);
        return super.onCreateOptionsMenu(menu);
    }

    /* Called whenever we call invalidateOptionsMenu() */
    @Override
    public boolean onPrepareOptionsMenu(Menu menu)
    {
        // If the nav drawer is open, hide action items related to the content
        // view
        boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
        menu.findItem(R.id.action_keyboard).setVisible(!drawerOpen);
        return super.onPrepareOptionsMenu(menu);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item)
    {
        /*
         * The action bar home/up should open or close the drawer.
         * ActionBarDrawerToggle will take care of this.
         */
        if (mDrawerToggle.onOptionsItemSelected(item))
        {
            return true;
        }

        // Handle your other action bar items...
        return super.onOptionsItemSelected(item);
    }

    private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
    {

        public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
        {
            super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
        }

        @Override
        public void onDrawerClosed(View view)
        {
            getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }

        @Override
        public void onDrawerOpened(View drawerView)
        {
            getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
            supportInvalidateOptionsMenu(); // creates call to
                                            // onPrepareOptionsMenu()
        }
    }

    //private void selectItem(int position)
    //{
    //  switch (position)
    //  {
    //      case 1:
    //          Intent a = new Intent(MainActivity.this, ControlActivity.class);
    //          startActivity(a);
    //          break;
    //      case 2:
    //          Intent b = new Intent(MainActivity.this, Tutorial.class);
    //          startActivity(b);
    //          break;
    //      default:
    //  }
    //}

    private class DrawerItemClickListener implements ListView.OnItemClickListener
    {

        @Override
        public void onItemClick(AdapterView<?> parent, View view, int position, long id)
        {
            // Highlight the selected item, update the title, and close the
            // drawer
            // update selected item and title, then close the drawer
            //mDrawerList.setItemChecked(position, true);
            // String text = "menu click... should be implemented";
            // Toast.makeText(MainActivity.this, text,
            // Toast.LENGTH_LONG).show();
            // // You should reset item counter
            //mDrawer.closeDrawer(mDrawerList);
            Intent intent = new Intent(MainActivity.this, Tutorial.class);
            startActivity(intent);

        }
    }

}

NOTE
I need it to work with activities & not with Fragments .

If you want your NavigationDrawer in all your activities you can launch from the NavigationDrawer, the best way to handle that is not to launch new Activity , but to replace Fragment .

Because if you launch a new Activity each time you'll need to implement your NavigationDrawer in all your subactivities. (easily doable with heritage though).

There is a nice tutorial here to play with "replace" Fragments with the NavigationDrawer :

http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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