【问题标题】:Behavior of CollapsingToolbarLayout in Separate FragmentCollapsingToolbarLayout 在单独片段中的行为
【发布时间】:2017-03-17 20:39:42
【问题描述】:
我需要实现与CollapsingToolbarLayout 行为类似的东西。使这个小部件不适合我的目的的两个主要区别:
这不是工具栏的一部分。它是一种通过位于工具栏正下方的片段进行膨胀的布局,但不能是工具栏的子级。
当我滚动屏幕时,我希望上部框架布局('header_fragment_container',其中填充了一个子片段),就像 CollapsingToolbarLayout 一样折叠。实际上,我希望将其替换为不同的布局。
是否有人知道可以在应用栏/工具栏之外处理这种可折叠性的适当小部件或第三方库?
这是片段目前存在的 xml:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<FrameLayout
android:id="@+id/header_fragment_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#fff"/>
<FrameLayout
android:id="@+id/content_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</ScrollView>
【问题讨论】:
标签:
android
material-design
android-collapsingtoolbarlayout
【解决方案1】:
您可以实现自定义解决方案 - 监听滚动变化并适当地进行布局修改:
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
private static final int THRESHOLD = 150;
private int scrolledDistance = 0;
private boolean toolbarVisible = true;
@Override
public void onScrollChange(View view, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (view.getScrollY() == 0) {
if(!toolbarVisible) {
toolbarVisible = true;
showToolbar();
}
} else {
if (toolbarVisible && scrolledDistance > THRESHOLD) {
toolbarVisible = false;
scrolledDistance = 0;
hideToolbar();
} else if (!toolbarVisible && scrolledDistance < -THRESHOLD) {
toolbarVisible = true;
scrolledDistance = 0;
showToolbar();
}
}
if ((toolbarVisible && scrollY - oldScrollY > 0) || (!toolbarVisible && scrollY - oldScrollY < 0)) {
scrolledDistance += scrollY - oldScrollY;
}
}
});
private void showToolbar() {
// show the Toolbar and make the necessary layout modifications
final ObjectAnimator animator = ObjectAnimator.ofFloat(toolbar, "y", 0);
animator.setDuration(1000);
animator.start();
}
private void hideToolbar() {
// hide the Toolbar and make the necessary layout modifications
final ObjectAnimator animator = ObjectAnimator.ofFloat(toolbar, "y", -getActionBarHeight());
animator.setDuration(1000);
animator.start();
}