How to get Toolbar from fragment?

后端 未结 10 1807
梦毁少年i
梦毁少年i 2020-11-28 01:45

I have ActionBarActivity with NavigationDrawer and use support_v7 Toolbar as ActionBar. In one of my fragments toolbar has custom view

相关标签:
10条回答
  • 2020-11-28 02:36

    I did it by using these steps.

    1. Set Title using below code in onCreateView of the main fragment.
    ((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Your title");
    
    
    1. For swapping between fragments i'm using bottom navigation bar which is implemented on MainActivity (Parent Activity) of the fragment. Even if you are using any button or menu item then you can change the title from onSelectedItemClickListener, just like i did in my case.
      public boolean onNavigationItemSelected(@NonNull MenuItem menuItem) {
          switch (menuItem.getItemId()){
              case R.id.menu_dashboard:
                  getSupportActionBar().setTitle("Dashboard");
                  fm.beginTransaction().hide(active).show(dashboardFragment).commit();
                  active = dashboardFragment;
                  return true;
              case R.id.menu_workshop:
                  getSupportActionBar().setTitle("Workshops");
                  fm.beginTransaction().hide(active).show(workshopFragment).commit();
                  active = workshopFragment;
                  return true;
           }
           return false;
      }
      
    0 讨论(0)
  • 2020-11-28 02:39

    You have two choices to get Toolbar in fragment

    First one

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

    and second one

    Toolbar toolbar = ((MainActivity) getActivity()).mToolbar;
    
    0 讨论(0)
  • 2020-11-28 02:40

    In case fragments should have custom view of ToolBar you can implement ToolBar for each fragment separately.

    add ToolBar into fragment_layout:

    <android.support.v7.widget.Toolbar
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="?attr/colorPrimaryDark"/>
    

    find it in 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);
    
            //set toolbar appearance
            toolbar.setBackground(R.color.material_blue_grey_800);
    
            //for crate home button
            AppCompatActivity activity = (AppCompatActivity) getActivity();
            activity.setSupportActionBar(toolbar);
            activity.getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    }
    

    menu listener could be created two ways: override onOptionsItemSelected in your fragment:

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

    or set listener for toolbar when create it in onCreateView():

    toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() {
                @Override
                public boolean onMenuItemClick(MenuItem menuItem) {
                    return false;
                }
            });
    
    0 讨论(0)
  • 2020-11-28 02:40
    toolbar = (Toolbar) getView().findViewById(R.id.toolbar);
    AppCompatActivity activity = (AppCompatActivity) getActivity();
    activity.setSupportActionBar(toolbar);
    
    0 讨论(0)
提交回复
热议问题