David Corsalini
David Corsalini

Reputation: 8208

Navigation Drawer: DrawerToggle not working, DrawerList not listening

I'm trying to use the navigation drawer, I've followed every step from the official developer website, still I have two problems:

Everything else works (even the open/close listener). Can you help me find the error?

import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.widget.DrawerLayout;    

public class ActTimerList extends FragmentActivity {


private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;


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

            ---
    configureActionBarAndNavigationDrawer();

    retrieveStuff();
}

private void configureActionBarAndNavigationDrawer(){

    mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
    mDrawerList = (ListView) findViewById(R.id.drawer_list);
    mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* 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 */
            ) {

        /** Called when a drawer has settled in a completely closed state. */
        public void onDrawerClosed(View view) {
            //Some code that works
        }

        /** Called when a drawer has settled in a completely open state. */
        public void onDrawerOpened(View drawerView) {
        }
    };
    // Set the drawer toggle as the DrawerListener
    mDrawerLayout.setDrawerListener(mDrawerToggle);
    mDrawerList.setOnItemClickListener(new ListView.OnItemClickListener() {

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

            Log.d("Drawer", "Stuff clicked: " + position);  
            mDrawerList.setItemChecked(position, true);
            getActionBar().setTitle(stuff[position].name);
            mDrawerLayout.closeDrawer(mDrawerList);


        }
    });


    getActionBar().setDisplayHomeAsUpEnabled(true);
    getActionBar().setHomeButtonEnabled(true);
}


@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 onOptionsItemSelected(MenuItem item) {
    // Pass the event to ActionBarDrawerToggle, if it returns
    // true, then it has handled the app icon touch event
    if (mDrawerToggle.onOptionsItemSelected(item)) {
        return true;
    }
    // Handle your other action bar items...

    return super.onOptionsItemSelected(item);
}



private void retrieveStuff() {

            query.findInBackground(new FindCallback<ParseObject>() { //Parse API to retrieve stuff, async
        public void done(List<ParseObject> stuffList, ParseException e) { //Callback for when the query completes
            if (e == null) {
            //This works, stuff is being loaded and the adapter is set correctly
                mDrawerList.setAdapter(new StuffAdapter(context,
                        R.layout.item_stuff, stuff));
                ---
            } 
        }
    });
}

Upvotes: 2

Views: 5358

Answers (3)

TanvirChowdhury
TanvirChowdhury

Reputation: 2445

in your selection method you are not launching your Fragment correctly

private void selectItem(int position)  
{    
     FragmentManager fragmentManager = getSupportFragmentManager();

     fragmentTrnsaction = fragmentManager.beginTransaction();

------>>> after this use

fragmentTrnsaction.add() or replace() method.

Upvotes: 0

mente
mente

Reputation: 2856

I don't see where you popular navigation drawer. Probably that's the problem? Consider following snippet in the end of drawer layout initialization

getFragmentManager().beginTransaction().add(R.id.left_drawer, new MenuFragment(), "menu").commit();

MenuFragment is my custom fragment that prepares drawer ui. Implementation is up to you. If you have a simple bunch of items in menu consider using ListFragment.

An example of your layout should have for your activity:

<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/drawer_layout"
    tools:context=".activities.MainActivity">

    <!-- Your awesome content here -->

    <FrameLayout android:id="@+id/left_drawer"
              android:layout_width="240dp"
              android:layout_height="match_parent"
              android:layout_gravity="start"
              android:choiceMode="singleChoice" />
</android.support.v4.widget.DrawerLayout>

Upvotes: 0

none
none

Reputation: 1757

Explain errors: what doesn't work exactly?

here are some my snippets:

    mDrawerToggle = new ActionBarDrawerToggle(
            this, 
            mDrawerLayout, 
            R.drawable.ic_drawer, 
            R.string.drawer_open,
            R.string.drawer_close) {

        public void onDrawerClosed(View view) {
            getSupportActionBar().setTitle(mTitle);
            supportInvalidateOptionsMenu();
        }

        public void onDrawerOpened(View view) {
            getSupportActionBar().setTitle(mDrawerTitle);
            supportInvalidateOptionsMenu();
        }
    };

    mDrawerLayout.setDrawerListener(mDrawerToggle);

And about ListView Listener:

    mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

And Listener:

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

And selectItem():

  private void selectItem(int position) {
    FragmentManager fragmentManager = getSupportFragmentManager();


    fragmentTrnsaction = fragmentManager.beginTransaction();

    switch (position) {

    case 0:
        <some code>
        break;
    case 1:
        <some code>
        break;

    case 2:
        <some code>
        break;

    case 3:
        <some code>
        break;

    default:
        break;
    }


    mDrawerList.setItemChecked(position, true);

    mDrawerLayout.closeDrawer(mDrawerList);
}

Upvotes: 1

Related Questions