I have a sidebar. It is done as follows:
MainActivity.java
// Methods, variables, etc... Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); setSupportActionBar(toolbar); drawer = (DrawerLayout) findViewById(R.id.drawer_layout); drawer.addDrawerListener(new ActionBarDrawerToggle( this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close){ @Override public void onDrawerSlide(View drawerView, float slideOffset) { mainActivityPresenter.loadMenuItems(MainActivity.this, MainActivity.this.menu); } }); navigationView = (NavigationView) findViewById(R.id.nav_view); navigationView.setNavigationItemSelectedListener(this); navigationView.setOnCreateContextMenuListener(this); //etc... The class also has a listener
@Override public void onCreateContextMenu(ContextMenu menu, View v, ContextMenu.ContextMenuInfo menuInfo){ this.menu = menu; } MainActivityPresenter.java
public void loadMenuItems(MainActivity mainActivity, ContextMenu menu){ if(Что-то там){doSmth();} } Well, the markup file itself:
<?xml version="1.0" encoding="utf-8"?> <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/drawer_layout" android:layout_width="match_parent" android:layout_height="match_parent" android:fitsSystemWindows="true" tools:openDrawer="start"> <include layout="@layout/app_bar_main" android:layout_width="match_parent" android:layout_height="match_parent" /> <android.support.design.widget.NavigationView android:id="@+id/nav_view" android:layout_width="wrap_content" android:layout_height="match_parent" android:layout_gravity="start" android:fitsSystemWindows="true" app:headerLayout="@layout/nav_header_main" app:menu="@menu/activity_main_drawer" /> </android.support.v4.widget.DrawerLayout> I need to change the menu after certain actions in the application. I decided to implement this by overriding the onDrawerSlide method, which, as I understand it, is called when the sidebar menu moves. This method calls another method of the class MainActivityPresenter, which should be a menu. How to do it right? I had a few ideas, but they all turned out to be unsuccessful. (Correctly, I understand that onCreateContextMenu is called when creating a menu in this case?