簡體   English   中英

帶有其他信息和viewpager的折疊工具欄

[英]Collapsing toolbar with Additional Information and viewpager

我想使用折疊工具欄效果設計類似上圖的布局,我已經嘗試過使用viewpager折疊工具欄,但是我只能添加Image和viewpgaer,我想添加以下信息,然后添加viewpager,請在這里幫助我任何人都有任何想法。

非常感謝您!

<?xml version="1.0" encoding="utf-8"?>

<android.support.design.widget.AppBarLayout
    android:id="@+id/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/collapsing_toolbar"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|exitUntilCollapsed"
        app:contentScrim="?attr/colorPrimary"
        app:expandedTitleMarginEnd="@dimen/collapsing_tool_bar_margin_end"
        app:expandedTitleMarginBottom="@dimen/collapsing_tool_bar_margin_bottom"
        app:expandedTitleMarginStart="@dimen/collapsing_tool_bar_margin_bottom">


         <ImageView
            android:id="@+id/coverImage"
            android:layout_width="match_parent"
            android:layout_height="200dp"
             app:layout_collapseMode="parallax"
            android:scaleType="centerCrop"/>

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            app:theme="@style/ActionBarThemeOverlay"
            app:popupTheme="@style/ActionBarPopupThemeOverlay"
            app:layout_collapseMode="pin"
            android:background="@drawable/gradient_bg" />


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

    <android.support.design.widget.CollapsingToolbarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_scrollFlags="scroll|enterAlways">

    <android.support.design.widget.TabLayout
        android:id="@+id/slidingTabs"
        android:fitsSystemWindows="true"
        android:layout_width="match_parent"
        app:tabMode="scrollable"
        app:tabGravity="fill"
        android:background="@color/transparent"
        android:layout_height="wrap_content"/>

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

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

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    android:layout_height="wrap_content">
</android.support.v4.view.ViewPager>

您可以在折疊的工具欄中添加其他視圖,例如:

<LinearLayout android:layout_width="wrap_content" android:layout_height="wrap_content"
                          android:id="@+id/header_layout"
                          android:orientation="vertical"
                          android:layout_marginLeft="20dp"
                          android:layout_marginBottom="10dp"
                          android:layout_gravity="bottom|left">
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                          android:text="Title"
                          android:textColor="#ffffff"
                          android:textSize="25dp"/>
                <TextView android:layout_width="wrap_content" android:layout_height="wrap_content"
                          android:text="subtitle"
                          android:textColor="#ffffff"/>
            </LinearLayout>

根據需要調整文本大小,邊距和其余部分。

但是,然后添加此代碼,以確保在工具欄折疊時您添加的布局淡入

headerLayout=(LinearLayout) findViewById(R.id.header_layout);

AppBarLayout appBarLayout = (AppBarLayout) findViewById(R.id.app_bar_layout);
    appBarLayout.addOnOffsetChangedListener(this);

並使您的活動類實現 AppBarLayout.OnOffsetChangedListener

然后,您的onOffsetChanged方法應該是這樣的。

 @Override
public void onOffsetChanged(AppBarLayout appBarLayout, int offset) {
    float scaleFactor=1+(offset*0.005f);
    if(!(scaleFactor<0)){

        headerLayout.setAlpha(scaleFactor);
    }
    if(scaleFactor<0.3){
        headerLayout.setAlpha(0);
    }

}

調整浮點值“ 0.005f”以獲得所需效果。

我希望這是有幫助的

我更改了主題以解決此問題:

您應該使用ThemeOverlay.AppCompat.Light;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM