CollapsingToolbarLayout setTitle() does not update unless collapsed

前端 未结 3 1492
既然无缘
既然无缘 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:19

    Okay I have a workaround while we wait for Google:

    1. Grab the gist from https://gist.githubusercontent.com/blipinsk/3f8fb37209de6d3eea99/raw/b13bd20ebb319d94399f0e2a0bedbff4c044356a/ControllableAppBarLayout.java (I'm not the original creator but kudos to original author). This adds a few methods to the AppBarLayout, namely expand and collapse

    2. In your method that calls setTitle():

    collapsingToolbar.setTitle("All Recent");
    getSupportActionBar().setTitle("All Recent");
    collapseThenExpand();
    
    1. Now create a collapseThenExpand() method:

    private void collapseThenExpand() {
      appbar.collapseToolbar();
    
      Handler h = new Handler();
      h.postDelayed(new Runnable() {
        @Override
        public void run() {
          appbar.expandToolbar(true);
        }
      }, 800);
    }
    

    Note you can turn off the expand animation by setting it to false.

提交回复
热议问题