Collapsing Toolbar only for one Fragment in Navigation View

前端 未结 5 1015
天命终不由人
天命终不由人 2021-01-02 03:47

The Problem

I have a navigation drawer with different fragments. There is a default toolbar every Fragment should use, except of one Fragment

5条回答
  •  离开以前
    2021-01-02 04:21

    You can easily get the Toolbar from your Fragment and then modify or change some property of that Toolbar inside the Fragment.

    To get the Toolbar from your Activity you might consider using this.

    Toolbar toolbar = (Toolbar) getActivity().findViewById(R.id.toolbar);
    

    Now you need to make the changes on the Toolbar in the onResume function and then undo the changes each time you return from the Fragment inside onStop function. Otherwise the changes made in the Fragment will be carried on to other fragments as well when switched to other Fragment from the navigation drawer.

    But in your case, I would recommend each Fragment should have their Toolbar so that it doesn't conflict with each other and can be modified as you need. And yes, remove the Toolbar from your Activity.

    So add the Toolbar in the layout of your Fragment like this.

    
    

    Then find it in the Fragment

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment, container, false);
        Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
    
        // Modify your Toolbar here. 
        // ... 
        // For example. 
        // toolbar.setBackground(R.color.red);
    
        // Create home button
        AppCompatActivity activity = (AppCompatActivity) getActivity();
        activity.setSupportActionBar(toolbar);
        activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    And Override the onOptionsItemSelected function.

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch(item.getItemId()){
            case android.R.id.home:
                getActivity().onBackPressed();
        }
        return super.onOptionsItemSelected(item);
    }
    

提交回复
热议问题