Question

In order to create a Navigation Drawer, and because it seemed confusing, I used Eclipse's automatically created template. However, this template creates code so that it displays the content of a String array adapter, and at the selection of an item a Fragment is instantiated in the attached Activity like this:

@Override
public void onNavigationDrawerItemSelected(int position)
{
   // update the main content by replacing fragments
   FragmentManager fragmentManager = getSupportFragmentManager();
   Fragment fragment = null;
   switch (position)
   {
     case 0:
        fragment = new FragmentType0();
        break;
     case 1:
        fragment = new FragmentType1();
        break;
     case 2:
        fragment = new FragmentType2();
        break;
     default:
        Log.w(this.getClass().getSimpleName(), "Reached Default in onNavigationDrawerItemSelected!");
        break;
  }
  if (fragment != null)
  {
     FragmentTransaction ft = fragmentManager.beginTransaction();
     ft.replace(R.id.container, fragment);
     ft.addToBackStack(null);
     ft.commit();
     mTitle = getString(((GetActionBarTitle) fragment).getActionBarTitleId());
     restoreActionBar();
  }

}

Because the generated template is the following:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
   mDrawerListView = (ListView) inflater.inflate(R.layout.fragment_navigation_drawer,  container, false);
   mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener()
   {
      @Override
      public void onItemClick(AdapterView<?> parent, View view, int position, long id)
      {
         selectItem(position);
      }
   });
   String[] drawerTitles = new String[] { getString(R.string.drawer_string_0),  getString(R.string.drawer_string_1), getString(R.string.drawer_string_2)};
   mDrawerListView.setAdapter(new ArrayAdapter<String> (getActionBar().getThemedContext(), android.R.layout.simple_list_item_1,
         android.R.id.text1, drawerTitles));
   mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
   return mDrawerListView;
}

So as you can see, the titles are given in the navigation drawer, displayed and specified as a String, and when you select an item, the callback is called on the Activity, at which based on position of selected item, the 'proper' Fragment is instantiated based on an if-else structure. This seems like a fairly fragile solution, but I can't seem to figure out how to make it better.

My hunch is something like using a FragmentStatePagerAdapter, but I'm at a loss, as I've never used one before, especially not in this particular context (I'm REALLY new to the navigation drawer, and I don't know how I would integrate it with the navigation drawer, so that it displays strings that correspond to each Fragment). Can anyone please provide advice on what to do here? It works, but it doesn't seem like the "proper" solution.

Was it helpful?

Solution

I'd reccomend that you stay away from the auto-generated code. A lot of the time, it isn't at all what you want.

I recommend you take a look at this tutorial. It explains the basics of the navigation drawer pretty well.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top