我使用的是最新版本的设计支持库 (23.1.1),当我使用带有 enterAlways
滚动标志的 CollapsingToolbarLayout 时遇到问题。基本上,当您向上滚动时, View 会出现,但如果还会在顶部留下一个空白区域。
普通 View :
MainActivity.java
public class MainActivity extends AppCompatActivity {
AppBarLayout appBar;
View expandedView;
Toolbar toolbar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
setTitle("");
initViews();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
private void initViews() {
appBar = (AppBarLayout) findViewById(R.id.appBar);
appBar.addOnOffsetChangedListener(appBarOffsetChangedListener);
expandedView = findViewById(R.id.expandedView);
RecyclerView rv = (RecyclerView) findViewById(R.id.rv);
rv.setLayoutManager(new LinearLayoutManager(this));
rv.setAdapter(new DummyAdapter());
}
private AppBarLayout.OnOffsetChangedListener appBarOffsetChangedListener = new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
int maxOffset = appBar.getTotalScrollRange();
verticalOffset = Math.abs(verticalOffset);
if(verticalOffset > maxOffset)
return;
float percentage = verticalOffset / (float) maxOffset;
if(expandedView!=null)
expandedView.setAlpha(1 - percentage);
}
};
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.media2359.fragmenttoolbarchange.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="@+id/appBar"
android:layout_width="match_parent"
android:layout_height="180dp"
android:background="@color/colorPrimaryDark"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="@+id/collapsingToolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways">
<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
</android.support.v7.widget.Toolbar>
<RelativeLayout
android:id="@+id/expandedView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="@dimen/toolbar_text_margin_left"
android:paddingTop="@dimen/toolbar_text_margin_top"
tools:background="@color/colorPrimaryDark">
<TextView
android:id="@+id/tvName"
style="@style/TextAppearance.AppCompat.Headline"
android:layout_width="@dimen/toolbar_text_width"
android:layout_height="wrap_content"
android:text="Hello" />
<TextView
android:id="@+id/tvTime"
style="@style/TextAppearance.AppCompat.Body1"
android:layout_width="@dimen/toolbar_text_width"
android:layout_height="wrap_content"
android:layout_below="@id/tvName"
android:layout_marginTop="7dp"
android:text="04 Feb, Tuesday evening" />
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="@+id/rv"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:listitem="@layout/item_dummy" />
</android.support.design.widget.CoordinatorLayout>
使用 enterAlwaysCollapsed
和 enterAlways
可以避免这个问题,但我希望返回完整 View ,因为在实际应用中,展开的部分要小得多。
我注意到的另一件事是,空白的高度等于工具栏的高度。
编辑 1
我用 snap
替换了 exitUntilCollapsed
然后没有任何空白,但工具栏没有固定和滚动
编辑 2
看起来这是设计库的问题:CollapsingToolbarLayout enterAlways not supported
最佳答案
可能是因为:
enterAlways
enterAlways
: The view will become visible when scrolling up. This flag is useful in cases when scrolling from the bottom of a list and wanting to expose the Toolbar as soon as scrolling up takes place.
也许你想试试这个:(标准方式)
app:layout_scrollFlags="scroll|exitUntilCollapsed"
老实说,在我的整个开发生涯中,我没有看到有人在 CollapsingToolbarLayout
中使用 enterAlways
。特别是,使用这两个标志:
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"
否则,这可能是一个错误,需要 Google 的工作人员对此进行解答。
关于Android设计库AppBar滚动行为enterAlways,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35204283/