0

I am using a bottom app bar with a Navigation Drawer. When I click on one item of the drawer, a new fragment is added on top of my Main activity. Inside this fragment I want to have a RecyclerView. When the fragment is attached, I want to see the normal menu that I am inflating on the MainActivity, but when an item is clicked on the recycler view I want to replace the menu with a custom one. Right now It´s just adding more items to be existing menu. How I can replace with a new menu, and make the Fab Button disappear or move to the right?

enter image description here

The button is just to simulate the click on my future RecyclerCiew.

public class CourseFragment extends Fragment {
private Menu mMenu;
private MenuInflater mInflater;


public CourseFragment() {
    // Required empty public constructor
}

@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setHasOptionsMenu(true);
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view= inflater.inflate(R.layout.fragment_course, container, false);
    Button btn=view.findViewById(R.id.button);



    btn.setOnClickListener(v->{
        mInflater.inflate(R.menu.bottomappbar_edit_remove_menu,mMenu);mMenu.
    });
    return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    mMenu=menu;
    mInflater=inflater;
    //inflater.inflate(R.menu.bottomappbar_main_menu,menu);
    //super.onCreateOptionsMenu(menu,inflater);
}
}
Rolando Azevedo
  • 193
  • 1
  • 5
  • 14

1 Answers1

1

In your RecyclerView you need to implement a OnClickListener that notify your fragment with the desired menu, and then you call invalidateOptionsMenu() to make onCreateOptionsMenu() run again and build the new menu.

Using your example with the button your code would look like this

private int menuId = R.menu.bottomappbar_main_menu; // Your initial menu
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
                         Bundle savedInstanceState) {
    // Inflate the layout for this fragment
    View view = inflater.inflate(R.layout.fragment_course, container, false);
    Button btn = view.findViewById(R.id.button);

    btn.setOnClickListener(v -> {
        this.menuId = R.menu.bottomappbar_another_menu; // another menu that you want to use
        getActivity().invalidateOptionsMenu(); // notify that menu needs to be changed
    });
    return view;

}

@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
    // Inflate your menu
    inflater.inflate(this.menuId, menu);
    super.onCreateOptionsMenu(menu, inflater);
}

Don't forget that you will need to create a new menu file with the options you want

  • Thanks it worked , but i had to remove the menu inflater on the Main Activity of the fragment. Do you know how i can move right or hide the Fab button when button is pressed? – Rolando Azevedo Jun 22 '19 at 20:21
  • See this answer https://stackoverflow.com/a/31094315/10709983 . You can move your FAB to left/right using onTouch() listener and View.animate() after you press your button – Jonathas Nascimento Jun 22 '19 at 21:31