abdfahim
abdfahim

Reputation: 2553

Navigation Drawer with Activities - Basic Concept

I have one basic question that I couldn't get hold in last couple of days (even after many example/explanation in googling and SOing).

I have 5 different Activity , each activity is independent of others, and each activity has it’s own layout file. Now, I want to add Navigation Drawer as my App Menu.

What i understand, 2 recommended ways are:

1) To merge those 5 activities into one MainActivity, and use 5 fragments to load 5 different layout. But it will be a hard for me to merge those 5 into 1. And won't there be any performance issue if I have all methods loaded in one Activity?

2) To have one BaseActivity containing drawer, and extend all other activity to Base Activity. But I have all my Activities extended to NavDrawer class (which contains drawer), but not working. Individually drawer is working fine (when I run NavDrawer only). Do I need to make any change to the layout xmls of my existing activities?

I am sorry if this is pretty much basic, but I am posting this after I failed to get hold the concept in 2 days!

I can attach my code if you want, but the drawer code is sort of basic thing, as per tutorial.

Thanks,

References I am using:
1) http://developer.android.com/training/implementing-navigation/nav-drawer.html 2) http://www.androidhive.info/2013/11/android-sliding-menu-using-navigation-drawer/

Upvotes: 0

Views: 933

Answers (1)

Kanth
Kanth

Reputation: 6751

I can suggest you to use ViewPager with actionbar tabs. Because with the help of viewpager, the same navigation draawer is visible for all the five fragments. Coming to the efficiency, ViewPager creates current page, besides it always prepares your next and previous pages also. So that it can be bit fast in showing the content while the user swipes pages. I always follow the same and I don't think it's inefficient.

Code snippet of one of my applications is below:

public class MainActivity extends ActionBarActivity {


    String[] titles;

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

    private CharSequence mDrawerTitle;
    private CharSequence mTitle;
    private String[] mListTitles;

    public PagerTabStrip titleStrip;


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

            viewPager = (ViewPager) findViewById(R.id.pager);

            TitleAdapter titleAdapter = new TitleAdapter(getSupportFragmentManager());
            viewPager.setAdapter(titleAdapter);
            viewPager.setCurrentItem(0);

            mTitle = mDrawerTitle = getTitle();
            mListTitles = getResources().getStringArray(R.array.drawerlist_array);
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mDrawerList = (ListView) findViewById(R.id.left_drawer);

            mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
            // sets up the drawer's list view with items and click listener
            mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mListTitles));
            mDrawerList.setOnItemClickListener(new DrawerItemClickListener());

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


            mDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
            mDrawerLayout, /* DrawerLayout object */
            R.drawable.ic_drawer, /* nav drawer image to replace 'Up' caret */
            R.string.drawer_open, /* "open drawer" description for accessibility */
            R.string.drawer_close /* "close drawer" description for accessibility */
            )
            {
                public void onDrawerClosed(View view)
                {
                    getSupportActionBar().setTitle(mTitle);

                }

                public void onDrawerOpened(View drawerView)
                {
                    getSupportActionBar().setTitle(mDrawerTitle);

                }
            };
            mDrawerLayout.setDrawerListener(mDrawerToggle);

            if (savedInstanceState == null)
            {
                selectItem(0);
            }


        }

        @Override
        public boolean onCreateOptionsMenu(Menu menu) {

            getMenuInflater().inflate(R.menu.main, menu);
            return true;
        }

        @Override
        public boolean onPrepareOptionsMenu(Menu menu)
        {

            boolean drawerOpen = mDrawerLayout.isDrawerOpen(mDrawerList);
            return super.onPrepareOptionsMenu(menu);
        }

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

                return false;
            }
        }


        class TitleAdapter extends FragmentPagerAdapter{
            private String titles[] = new String[]{"Expenses","Savings","Income"};
            private Fragment frags[] = new Fragment[titles.length]; 

            public TitleAdapter(FragmentManager fm) {
                    super(fm);
                    frags[0] = new Fragment1();
                    frags[1] = new Fragment2();
                    frags[2] = new Fragment3();
            }

            @Override
            public CharSequence getPageTitle (int position){
                    Log.v("TitleAdapter - getPageTitle=", titles[position]);
                    return titles[position];
            }

            @Override
            public Fragment getItem(int position) {
                    Log.v("TitleAdapter - getItem=", String.valueOf(position));
                    return frags[position];
            }

            @Override
            public int getCount() {
                    return frags.length;
            }

    }



        /* The click listner for ListView in the navigation drawer */
        private class DrawerItemClickListener implements ListView.OnItemClickListener
        {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id)
            {
                selectItem(position);
            }
        }

        private void selectItem(int position)
        {

            // update selected item and title, then close the drawer
            mDrawerList.setItemChecked(position, true);
            setTitle(mListTitles[position]);
            mDrawerLayout.closeDrawer(mDrawerList);
        }

        @Override
        public void setTitle(CharSequence title)
        {
            mTitle = title;
            getSupportActionBar().setTitle(mTitle);
        }

        /**
         * When using the ActionBarDrawerToggle, you must call it during
         * onPostCreate() and onConfigurationChanged()...
         */

        @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);
            // Pass any configuration change to the drawer toggls
            mDrawerToggle.onConfigurationChanged(newConfig);
        }


    }

Upvotes: 1

Related Questions