Search code examples
androidandroid-recyclerviewcategoriesnavigation-drawerpaytm

Drawer functionality in android just like paytm


I want to implement drawer functionality in my android application just like flipkart can anyone help how to show subcategories in the same drawer on the click of respected category


Solution

  • Try to use Expandable listview in drawer like this

    https://github.com/msahakyan/expandable-navigation-drawer

    public class MainActivity extends ActionBarActivity {
    
        private DrawerLayout mDrawerLayout;
        private ActionBarDrawerToggle mDrawerToggle;
        private String mActivityTitle;
    
        private ExpandableListView mExpandableListView;
        private ExpandableListAdapter mExpandableListAdapter;
        private List<String> mExpandableListTitle;
        private Map<String, List<String>> mExpandableListData;
        private TextView mSelectedItemView;
    
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
    
            mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
            mActivityTitle = getTitle().toString();
    
            mExpandableListView = (ExpandableListView) findViewById(R.id.navList);
            mSelectedItemView = (TextView) findViewById(R.id.selected_item);
    
            LayoutInflater inflater = getLayoutInflater();
            View listHeaderView = inflater.inflate(R.layout.nav_header, null, false);
            mExpandableListView.addHeaderView(listHeaderView);
    
            mExpandableListData = ExpandableListDataSource.getData(this);
            mExpandableListTitle = new ArrayList(mExpandableListData.keySet());
    
            addDrawerItems();
            setupDrawer();
    
            getSupportActionBar().setDisplayHomeAsUpEnabled(true);
            getSupportActionBar().setHomeButtonEnabled(true);
        }
    
        private void addDrawerItems() {
            mExpandableListAdapter = new CustomExpandableListAdapter(this, mExpandableListTitle, mExpandableListData);
            mExpandableListView.setAdapter(mExpandableListAdapter);
            mExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
                @Override
                public void onGroupExpand(int groupPosition) {
                    getSupportActionBar().setTitle(mExpandableListTitle.get(groupPosition).toString());
                    mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString());
                }
            });
    
            mExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
                @Override
                public void onGroupCollapse(int groupPosition) {
                    getSupportActionBar().setTitle(R.string.film_genres);
                    mSelectedItemView.setText(R.string.selected_item);
                }
            });
    
            mExpandableListView.setOnChildClickListener(new ExpandableListView.OnChildClickListener() {
                @Override
                public boolean onChildClick(ExpandableListView parent, View v,
                                            int groupPosition, int childPosition, long id) {
                    String selectedItem = ((List) (mExpandableListData.get(mExpandableListTitle.get(groupPosition))))
                        .get(childPosition).toString();
                    getSupportActionBar().setTitle(selectedItem);
                    mSelectedItemView.setText(mExpandableListTitle.get(groupPosition).toString() + " -> " + selectedItem);
                    mDrawerLayout.closeDrawer(GravityCompat.START);
                    return false;
                }
            });
        }
    
        private void setupDrawer() {
            mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
    
                /** Called when a drawer has settled in a completely open state. */
                public void onDrawerOpened(View drawerView) {
                    super.onDrawerOpened(drawerView);
                    getSupportActionBar().setTitle(R.string.film_genres);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
    
                /** Called when a drawer has settled in a completely closed state. */
                public void onDrawerClosed(View view) {
                    super.onDrawerClosed(view);
                    getSupportActionBar().setTitle(mActivityTitle);
                    invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
                }
            };
    
            mDrawerToggle.setDrawerIndicatorEnabled(true);
            mDrawerLayout.setDrawerListener(mDrawerToggle);
        }
    
        @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) {
            // Inflate the menu; this adds items to the action bar if it is present.
            getMenuInflater().inflate(R.menu.menu_main, menu);
            return true;
        }
    
        @Override
        public boolean onOptionsItemSelected(MenuItem item) {
            // Handle action bar item clicks here. The action bar will
            // automatically handle clicks on the Home/Up button, so long
            // as you specify a parent activity in AndroidManifest.xml.
            int id = item.getItemId();
    
            //noinspection SimplifiableIfStatement
            if (id == R.id.action_settings) {
                return true;
            }
    
            // Activate the navigation drawer toggle
            if (mDrawerToggle.onOptionsItemSelected(item)) {
                return true;
            }
    
            return super.onOptionsItemSelected(item);
        }
    }