CollapsingToolbarLayout可以定义何时崩溃?

时间:2017-09-06 16:44:59

标签: android android-collapsingtoolbarlayout

我有一个正在运行的CollapsingToolbarLayout,我看到的唯一问题是当展开时显示的内容在向下滚动时快速消失,并且想知道是否有可能定义内容应该消失的道路的哪一点并且appbar出现。看起来好像在拼贴~25%之后它已经隐藏了内容并且只显示了appbar的颜色。在Google Play应用中,您可以看到,当几乎全部崩溃时,这种情况发生得非常晚。

3 个答案:

答案 0 :(得分:0)

创建一个实现 AppBarLayout.OnOffsetChangedListener 的类并覆盖 onOffsetChanged 方法

<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"
    tools:ignore="RtlHardcoded"
    >

    <android.support.design.widget.AppBarLayout
        android:id="@+id/main.appbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/ThemeOverlay.AppCompat.Dark.ActionBar"
        >

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/main.collapsing"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            app:layout_scrollFlags="scroll|exitUntilCollapsed|snap"
            >

            <ImageView
                android:id="@+id/main.imageview.placeholder"
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:scaleType="centerCrop"
                android:src="@drawable/quila2"
                android:tint="#11000000"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.9"
                />

            <FrameLayout
                android:id="@+id/main.framelayout.title"
                android:layout_width="match_parent"
                android:layout_height="100dp"
                android:layout_gravity="bottom|center_horizontal"
                android:background="@color/primary"
                android:orientation="vertical"
                app:layout_collapseMode="parallax"
                app:layout_collapseParallaxMultiplier="0.3"
                >

                <LinearLayout
                    android:id="@+id/main.linearlayout.title"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_gravity="center"
                    android:orientation="vertical"
                    >

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:gravity="bottom|center"
                        android:text="@string/quila_name"
                        android:textColor="@android:color/white"
                        android:textSize="30sp"
                        />

                    <TextView
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_gravity="center_horizontal"
                        android:layout_marginTop="4dp"
                        android:text="@string/quila_tagline"
                        android:textColor="@android:color/white"
                        />

                </LinearLayout>
            </FrameLayout>
        </android.support.design.widget.CollapsingToolbarLayout>
    </android.support.design.widget.AppBarLayout>

    <android.support.v7.widget.Toolbar
        android:id="@+id/main.toolbar"
        android:layout_width="match_parent"
        android:layout_height="?attr/actionBarSize"
        android:background="@color/primary"
        app:layout_anchor="@id/main.framelayout.title"
        app:theme="@style/ThemeOverlay.AppCompat.Dark"
        app:title=""
        >

        <LinearLayout
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:orientation="horizontal"
            >

            <Space
                android:layout_width="@dimen/image_final_width"
                android:layout_height="@dimen/image_final_width"
                />

            <TextView
                android:id="@+id/main.textview.title"
                android:layout_width="wrap_content"
                android:layout_height="match_parent"
                android:layout_marginLeft="8dp"
                android:gravity="center_vertical"
                android:text="@string/quila_name2"
                android:textColor="@android:color/white"
                android:textSize="20sp"
                />

        </LinearLayout>
    </android.support.v7.widget.Toolbar>

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

更改 PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR PERCENTAGE_TO_HIDE_TITLE_DETAILS 以获得所需效果。

public class MainActivity extends AppCompatActivity implements AppBarLayout.OnOffsetChangedListener {

private static final float PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR  = 0.9f;
private static final float PERCENTAGE_TO_HIDE_TITLE_DETAILS     = 0.3f;
private static final int ALPHA_ANIMATIONS_DURATION              = 200;

private boolean mIsTheTitleVisible          = false;
private boolean mIsTheTitleContainerVisible = true;

private LinearLayout mTitleContainer;
private TextView mTitle;
private AppBarLayout mAppBarLayout;
private Toolbar mToolbar;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    bindActivity();
    mAppBarLayout.addOnOffsetChangedListener(this);
    mToolbar.inflateMenu(R.menu.menu_main);
    startAlphaAnimation(mTitle, 0, View.INVISIBLE);
}

private void bindActivity() {
    mToolbar        = (Toolbar) findViewById(R.id.main_toolbar);
    mTitle          = (TextView) findViewById(R.id.toolbar_title);
    mTitleContainer = (LinearLayout) findViewById(R.id.linearlayout_title);
    mAppBarLayout   = (AppBarLayout) findViewById(R.id.appbar);
}

@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
    int maxScroll = appBarLayout.getTotalScrollRange();
    float percentage = (float) Math.abs(offset) / (float) maxScroll;
    handleAlphaOnTitle(percentage);
    handleToolbarTitleVisibility(percentage);
}

private void handleAlphaOnTitle(float percentage) {
    if (percentage >= PERCENTAGE_TO_HIDE_TITLE_DETAILS) {
        if(mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
            mIsTheTitleContainerVisible = false;
        }
    } else {
        if (!mIsTheTitleContainerVisible) {
            startAlphaAnimation(mTitleContainer, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
            mIsTheTitleContainerVisible = true;
        }
    }
}

private void handleToolbarTitleVisibility(float percentage) {
    if (percentage >= PERCENTAGE_TO_SHOW_TITLE_AT_TOOLBAR) {
        if(!mIsTheTitleVisible) {
            startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.VISIBLE);
            mIsTheTitleVisible = true;
        }
    } else {
        if (mIsTheTitleVisible) {
            startAlphaAnimation(mTitle, ALPHA_ANIMATIONS_DURATION, View.INVISIBLE);
            mIsTheTitleVisible = false;
        }
    }
}

public static void startAlphaAnimation (View v, long duration, int visibility) {
    AlphaAnimation alphaAnimation = (visibility == View.VISIBLE) ? new AlphaAnimation(0f, 1f) : new AlphaAnimation(1f, 0f);
    alphaAnimation.setDuration(duration);
    alphaAnimation.setFillAfter(true);
    v.startAnimation(alphaAnimation);
}

}

答案 1 :(得分:0)

我会举例说明我是如何实现你所需要的。

由于你希望内容在例如60%之后开始代替,首先,声明一个像这样的变量

private static final int PERCENTAGE_TO_SHOW_ELEMENT = 60;

然后你必须声明一个变量来知道你的元素是否已被隐藏,以及一个跟踪滚动百分比的变量

private int mMaxScrollSize;
private boolean mIsElementHidden;

之后,您必须向appBarLayout添加名为addOnOffsetChangedListener的侦听器,并覆盖方法onOffsetChanged:

 yourAppBar.addOnOffsetChangedListener(this);

在方法onOffsetChanged中,使用以下代码:

@Override
    public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            if (mMaxScrollSize == 0)
                mMaxScrollSize = appBarLayout.getTotalScrollRange();

            int currentScrollPercentage = (Math.abs(verticalOffset)) * 100
                    / mMaxScrollSize;

            if (currentScrollPercentage >= PERCENTAGE_TO_SHOW_ELEMENT) {
                if (!mIsElementHidden) {
                    mIsElementHidden= true;
                    ViewCompat.animate(yourView).scaleY(0).scaleX(0).start();
                    ViewCompat.animate(anotherView).scaleY(0).scaleX(0).start();


                }
            }

            if (currentScrollPercentage < PERCENTAGE_TO_SHOW_ELEMENT) {
                if (mIsElementHidden) {
                    mIsElementHidden= false;
                    ViewCompat.animate(yourView).scaleY(1).scaleX(1).start();
                    ViewCompat.animate(anotherView).scaleY(1).scaleX(1).start();

                }
            }
        } else if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            //You can change something if your layout is different in LANDSCAPE
        }

    }

您可以使用变量PERCENTAGE_TO_SHOW_ELEMENT来决定何时使您的视图显示或消失。

答案 2 :(得分:0)

我找到了解决方案。你没有义务需要java代码,你可以使用样式属性 scrimVisibleHeightTrigger 来定义它,但请记住,使用的值应该大于动作栏的大小,遗憾的是使用?acionBarSize不起作用,所以你必须考虑appbar的高度并添加一些DP。

<android.support.design.widget.CollapsingToolbarLayout
    android:id="@+id/collapsing_toolbar"
    style="@style/Widget.MyCustomWidget.CollapsingHeader"

<style name="Widget.MyCustomWidget.CollapsingHeader" parent="@style/Widget.Design.CollapsingToolbar">
    <item name="scrimAnimationDuration">200</item>
    <item name="scrimVisibleHeightTrigger">1dp</item>        
</style>