Android toolbar show title and subtitle only when AppBarLayout collepsed

夙愿已清 提交于 2019-12-06 03:33:31

问题


I have activity with AppBarLayout ,CollapsingToolbarLayout and toolbar. Setting title and subtitle from code. Initially i want toolbar hidden and show when Appbar layout collapsed, With my code its working (toolbar hide initially) but its showing toolbar title and subtitle always. How do i show title only when appbar layout collapse completely

<android.support.design.widget.AppBarLayout
    android:id="@+id/app_bar"
    android:layout_width="match_parent"
    android:layout_height="@dimen/app_bar_height"
    android:fitsSystemWindows="true"
    android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.design.widget.CollapsingToolbarLayout
        android:id="@+id/toolbar_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:fitsSystemWindows="true"
        app:titleEnabled="false"
        app:contentScrim="?attr/colorPrimary"
        app:layout_scrollFlags="scroll|exitUntilCollapsed">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:layout_collapseMode="pin"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

    </android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>

Setting title and subtitle

 Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
    getSupportActionBar().setDisplayHomeAsUpEnabled(true);
    getSupportActionBar().setDisplayShowHomeEnabled(true);
    getSupportActionBar().setTitle("Title");
    getSupportActionBar().setSubtitle("sutitle");


回答1:


A simple AppBarLayout.OnOffsetChangedListener should do the trick using only built-in views:

AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar);
appBarLayout.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener {
    @Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
            ActionBar actionBar = getSupportActionBar();
            boolean toolbarCollapsed = Math.abs(offset) >= appBarLayout.getTotalScrollRange();
            actionBar.setTitle(toolbarCollapsed ? yourTitle : "");
            actionBar.setSubtitle(toolbarCollapsed ? yourSubTitle : "");
        }
});

(This code originally was written in C# (Xamarin), not Java, so minor modifications may be needed)




回答2:


I solve my problem using ControllableAppBarLayout in xml and handle its EXPAND, COLLAPSED, IDEAL event to show/ to set my TITLE and SUBTITLE using following approach.

 ControllableAppBarLayout appBarLayout = (ControllableAppBarLayout) findViewById(R.id.app_bar);
    appBarLayout.setOnStateChangeListener(new ControllableAppBarLayout.OnStateChangeListener() {

        @Override
        public void onStateChange(ControllableAppBarLayout.State toolbarChange) {
            switch (toolbarChange) {

                case COLLAPSED: {
                    Log.i(TAG, "COLLAPSED2");
                    if (mProfileDetails != null) {
                        getSupportActionBar().setTitle(mProfileDetails.userDetails.userFullname);
                        getSupportActionBar().setSubtitle(Html.fromHtml("<small>" + mProfileDetails.userDetails.headline + "</small>"));
                    }
                    break;
                }
                case EXPANDED:
                    Log.i(TAG, "EXPANDED");
                    getSupportActionBar().setTitle("");
                    getSupportActionBar().setSubtitle("");
                    break;

                case IDLE: // Just fired once between switching states
                    Log.i(TAG, "IDLE");
                    getSupportActionBar().setTitle("");
                    getSupportActionBar().setSubtitle("");
                    break;
            }
        }
    });


来源:https://stackoverflow.com/questions/35241698/android-toolbar-show-title-and-subtitle-only-when-appbarlayout-collepsed

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!