我正在使用最新版本的设计支持库(23.1.1),当我使用带有enterAlways
滚动标记的CollapsingToolbarLayout时,我遇到了一个问题。基本上,当您向上滚动时,会出现视图,但如果还在顶部留下空白空间。
普通视图:
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
可以避免此问题,但我希望全视图可以回来,因为在实际应用中,展开的部分会更小。
我注意到的另一件事是,空白的高度等于工具栏的高度。
编辑1
我将exitUntilCollapsed
替换为snap
,然后没有任何空白区域,但工具栏没有固定和滚动
编辑2
看起来这是设计库的一个问题:CollapsingToolbarLayout enterAlways not supported
答案 0 :(得分:1)
也许那是因为:
enterAlways
enterAlways
:向上滚动时,视图会变为可见。 此标志 在从列表底部滚动时很有用 想要在向上滚动时公开工具栏。
也许你想试试这个:(标准方式)
app:layout_scrollFlags="scroll|exitUntilCollapsed"
老实说,在我的整个发展生涯中,我没有看到有人在enterAlways
中使用CollapsingToolbarLayout
。特别是,有这两个标志:
app:layout_scrollFlags="scroll|exitUntilCollapsed|enterAlways"
否则,这可能是一个错误,需要 Google 的工作人员来回答这个问题。