CollapsingToolbarLayout のタイトルは折りたたまれたときのみ表示する 質問する

CollapsingToolbarLayout のタイトルは折りたたまれたときのみ表示する 質問する

試してみましたがsetExpandedTitleColorsetCollapsedTitleColor透明への切り替え)、うまくいきませんでした。探している機能を実行する組み込みメソッドも見つかりません。

CollapsingToolbarLayout が完全に折りたたまれている場合にのみタイトルを表示し、それ以外の場合は非表示にする必要があります。

何かヒントはありますか?

ベストアンサー1

いつ折りたたむか展開するかを決定し、タイトルを設定するためにOnOffsetChangedListenerを追加できます。AppBarLayoutCollapsingToolbarLayout

ジャワ

final CollapsingToolbarLayout collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.collapsingToolbarLayout);
AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
    boolean isShow = true;
    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(" ");//careful there should a space between double quote otherwise it wont work 
            isShow = false;
        }
    }
});

コトリン

var isShow = true
var scrollRange = -1
appBarLayout.addOnOffsetChangedListener(AppBarLayout.OnOffsetChangedListener { barLayout, verticalOffset ->
    if (scrollRange == -1){
        scrollRange = barLayout?.totalScrollRange!!
    }
    if (scrollRange + verticalOffset == 0){
        collapsingToolbarLayout.title = "Title Collapse"
        isShow = true
    } else if (isShow){
        collapsingToolbarLayout.title = " " //careful there should a space between double quote otherwise it wont work
        isShow = false
    }
})

おすすめ記事