CollapsingToolbarLayout setTitle() does not update unless collapsed

前端 未结 3 1493
既然无缘
既然无缘 2020-12-01 10:55

With the new Design Library, we\'re supposed to set the toolbar title on the CollapsingToolbarLayout, not the Toolbar itself(at least when using th

3条回答
  •  半阙折子戏
    2020-12-01 11:22

    In my solution I had to set the title for both the toolbar and the collapsing toolbar for it to work.

    So in OnCreate:

        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    
        collapsingToolbar = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    

    And then further down when I switch fragments I set the title for both when a tab is selected:

                public boolean onNavigationItemSelected(@NonNull MenuItem item) {
                    FragmentManager fm = getSupportFragmentManager();
                    FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
    
                    //TAB1 - THE DEFAULT TAB
                    switch (item.getItemId()) {
    
                            case R.id.tab_rooms:
                                toolbar.setTitle("My Title");
                                collapsingToolbar.setTitle("My Title");
                                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                fragment = new RoomsFragment();
                                transaction.replace(R.id.fragment_container, fragment);
                                transaction.addToBackStack(null);
                                transaction.commit();
    
                                return true;
    
                            case R.id.tab_shisha:
                                toolbar.setTitle("My Title2");
                                collapsingToolbar.setTitle("My Title2");
                                fm.popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
                                fragment = new ShishaFragment();
                                transaction.replace(R.id.fragment_container, fragment);
                                transaction.addToBackStack(null);
                                transaction.commit();
                                return true;
                              }
    
                        return false;
                    }
    

    Hope that helps someone!

提交回复
热议问题