更新时间:2023-01-05 21:06:21
一个简单的AppBarLayout.OnOffsetChangedListener
应该仅使用内置视图来解决问题:
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 : "");
}
});
(此代码最初是用C#(Xamarin)而不是Java编写的,因此可能需要进行少量修改)
(This code originally was written in C# (Xamarin), not Java, so minor modifications may be needed)