android hide toolbar in specific fragment

前端 未结 9 1262
既然无缘
既然无缘 2021-01-31 08:20

I have a problem that I don\'t know how to solve. How do you hide a toolbar in a specific fragment, I have already been searching around on the internet and what I found was com

相关标签:
9条回答
  • 2021-01-31 08:50

    Create an interface in the fragment and use it to tell the parent activity to hide the toolbar.

    Add these lines to your fragment:

    private OnEventListener listener;
    
    public interface OnEventListener {
    
        void hideToolbar() ;
    }
    
    public void setOnEventListener(OnEventListener listener) {
    
        this.listener = listener;
    }
    

    After creating your fragment in the main activity add:

        myFragment.setOnEventListener(new MyFragment.OnEventListener() {
            @Override
            public void hideToolbar() {
    
                getSupportActionBar().hide();
            }
        });
    

    Whenever you need to hide the toolbar execute:

    listener.hideToolbar();
    

    from inside your fragment.

    0 讨论(0)
  • 2021-01-31 08:53

    If you are using the new Navigation Component, add this while setting up the toolbar

    navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
       @Override
       public void onDestinationChanged(@NonNull NavController controller,
               @NonNull NavDestination destination, @Nullable Bundle arguments) {
           if(destination.getId() == R.id.full_screen_destination) {
               toolbar.setVisibility(View.GONE);
               bottomNavigationView.setVisibility(View.GONE);
           } else {
               toolbar.setVisibility(View.VISIBLE);
               bottomNavigationView.setVisibility(View.VISIBLE);
           }
       }
    });
    
    0 讨论(0)
  • 2021-01-31 08:55

    In the fragment's onCreate method call:
    ((AppCompatActivity) getActivity()).getSupportActionBar().hide();
    Replace AppCompateActivity with the activity class you used.

    Edited:

    You could simply use the onResume method to call hide() and the onStop method to call show() as suggested in some of the comments.

    @Override
    public void onResume() {
        super.onResume();
        ((AppCompatActivity)getActivity()).getSupportActionBar().hide();
    }
    
    @Override
    public void onStop() {
        super.onStop();
        ((AppCompatActivity)getActivity()).getSupportActionBar().show();
    }
    
    0 讨论(0)
提交回复
热议问题