【问题标题】:How to achieve text poping into the toolbar when scrolling, like in the Twitter app如何在滚动时实现文本弹出到工具栏,就像在 Twitter 应用程序中一样
【发布时间】:2016-06-23 01:57:16
【问题描述】:

考虑在工具栏中制作文本过渡/动画,如下面的 Twitter 应用程序所示:当用户名位于工具栏后面时,它会“弹出”到工具栏中。

或者,使文本滚动到工具栏后面并停留在那里。

【问题讨论】:

    标签: android android-toolbar android-coordinatorlayout android-collapsingtoolbarlayout


    【解决方案1】:

    您可以将 OnOffsetChangedListener 添加到 AppBarLayout 以确定 CollapsingToolbarLayout 何时折叠或展开并设置其标题。

    final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar);
    collapsingToolbarLayout.setTitle(" ");
    
    AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
    appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        boolean isShow = false;
        int scrollRange = -1;
    
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
            if (scrollRange == -1) {
                scrollRange = appBarLayout.getTotalScrollRange();
            }
            if (scrollRange + verticalOffset == 0) {
                collapsingToolbarLayout.setTitle("Title");
                isShow = true;
            } else if(isShow) {
                collapsingToolbarLayout.setTitle(" ");//carefull there should a space between double quote otherwise it wont work
                isShow = false;
            }
        }
    });
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-01-17
      • 1970-01-01
      相关资源
      最近更新 更多