Android SupportActionBar does not refresh title

随声附和 提交于 2019-12-20 02:15:31

问题


I have a problem with refreshing ActionBar title. The application is rather simple, currently it only have one activity: `

    <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: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>

    <include layout="@layout/content_count_scrolling"/>

    <android.support.design.widget.FloatingActionButton
        android:id="@+id/fabReset"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_margin="@dimen/fab_margin"
        android:src="@android:drawable/ic_dialog_alert"
        app:layout_anchor="@id/app_bar"
        app:layout_anchorGravity="bottom|end"/>

</android.support.design.widget.CoordinatorLayout>

The layout content_count_scrolling contains only NestedScrollView with RecyclerView inside.

My case is, when I enter a digit into the EditText in any of RecyclerView's row, it sets a value in the data model as well as recalculate the total value (sum from all rows). This total value should be set as ActionBar.title. To do this I am also using RxBus. MainActivity as below:

public class MainActivity extends AppCompatActivity {
    // ....
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main_scrolling);
        toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        toolbar.setTitle("");

        mRxBus = getRxBusSingleton();        
        // ....
    }
}

public void refreshActionBarTitle() {
    String total = Data.getTotal();
    Timber.d("Refresh bar title with value="+total);

    toolbar.setTitle("");
    setSupportActionBar(toolbar);
    toolbar.setTitle("Title "+total);

    // testes also with:
    // getSupportActionBar().setTitle("Title "+total);

    Timber.d("Title after refresh: " + getSupportActionBar().getTitle());
}

`

Method refreshActionBarTitle() is triggered by RxBus. Once when method is triggered, the title of the ActionBar also is set (checked with logs and debugger). The issue is, that ActionBar is not invalidated and redrawn. This is done only after rotate the screen, which is obvious.

So, please help me with correct invalidate the toolbar. I have to notice also, that this event will be triggered after avery change in EditText, not only after changing the focus, so i cannot invalidate eg. whole screen.

I have checked also advices from Toolbar (SupportActionBar) title changes to app name on orientation change, but they did not help me.


回答1:


Fixed. I should use CollapsingToolbarLayout.setTitle() method. This one works



来源:https://stackoverflow.com/questions/38672418/android-supportactionbar-does-not-refresh-title

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