Android Navigation Drawer Activity



Basically I have a navigation drawer base class which will be extended by multiple activities. Here is the code on how I set up the navigation drawer in Android:



public class NavigationDrawer extends Activity {
Context context;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private ActionBarDrawerToggle actionBarDrawerToggle;

protected DrawerLayout fullLayout;
protected FrameLayout actContent;

@Override
public void setContentView(final int layoutResID) {
fullLayout = (DrawerLayout) getLayoutInflater().inflate(
R.layout.navigation_drawer, null); // Your base layout here
actContent = (FrameLayout) fullLayout.findViewById(R.id.act_content);
getLayoutInflater().inflate(layoutResID, actContent, true);
super.setContentView(fullLayout);

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

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

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

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

getActionBar().setDisplayHomeAsUpEnabled(true);

// just styling option add shadow the right edge of the drawer
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
drawerListView.setOnItemClickListener(new DrawerItemClickListener());
}

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

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

private class DrawerItemClickListener implements
ListView.OnItemClickListener {
@Override
public void onItemClick(AdapterView parent, View view, int position,
long id) {
switch (position) {
case 1:
Intent dashboardIntent = new Intent(context, Dashboard.class);
startActivity(dashboardIntent);
break;
case 2:
break;
}
drawerLayout.closeDrawer(drawerListView);
}
}
}


And the XML file for the navigation drawer:



<android.support.v4.widget.DrawerLayout xmlns:android="http://ift.tt/nIICcg"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<!-- The navigation drawer -->

<ListView
android:id="@+id/left_drawer"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#111" />

<FrameLayout
android:id="@+id/act_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

</FrameLayout>
</android.support.v4.widget.DrawerLayout>


When I slide the navigation drawer out, it did shows the menu items. However, when I try to click on either one of the menu item, the drawer just slide back to hide but not selecting any menu items.


I wonder which part of my codes went wrong. Thanks in advance.


No comments:

Post a Comment