Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Collapsing ToolBar Layout with viewpager

I am using CollapsingBarLayout with viewpager and the fragments of viewpager are having listview, gridview.

Here is my code:

    <?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:id="@+id/main_content"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bigsteptech.seandroidnativeapp.classes.modules.common.ViewGroupEvent">

    <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:fitsSystemWindows="true">

        <android.support.design.widget.CollapsingToolbarLayout
            android:id="@+id/collapsing_toolbar"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            app:layout_scrollFlags="scroll|exitUntilCollapsed"
            android:fitsSystemWindows="true"
            app:contentScrim="?attr/colorPrimary"
            app:expandedTitleMarginEnd="64dp"
            app:expandedTitleMarginStart="48dp"
            app:expandedTitleTextAppearance="@style/TransparentText">

            <FrameLayout
                android:id="@+id/carouselLayout"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                app:layout_collapseMode="parallax">

                <ImageView
                    android:id="@+id/coverImage"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:scaleType="centerCrop"/>

                <LinearLayout
                    android:layout_width="match_parent"
                    android:gravity="bottom"
                    android:orientation="vertical"
                    android:layout_gravity="bottom"
                    android:padding="@dimen/profile_image_margin"
                    android:background="@drawable/gradient_bg"
                    android:layout_height="wrap_content">

                    <TextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/profile_image_margin"
                        android:textSize="@dimen/text_size_xlarge"
                        android:textStyle="bold"
                        android:textColor="@color/white"
                        android:id="@+id/content_title"/>

                    <RelativeLayout
                        android:layout_width="match_parent"
                        android:layout_marginLeft="@dimen/profile_image_margin"
                        android:layout_height="wrap_content">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:textColor="@color/white"
                            android:textSize="@dimen/text_size_medium"
                            android:id="@+id/category_title"/>

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:textStyle="bold"
                            android:layout_toRightOf="@+id/category_title"
                            android:layout_marginLeft="@dimen/profile_image_margin"
                            android:textColor="@color/white"
                            android:textSize="@dimen/text_size_medium"
                            android:id="@+id/memberCount"/>

                    </RelativeLayout>


                </LinearLayout>

            </FrameLayout>

            <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"
                android:background="@drawable/gradient_bg"
                app:layout_collapseMode="pin" />

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

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_gravity="fill_vertical"
        android:isScrollContainer="true"
        app:layout_behavior="@string/appbar_scrolling_view_behavior">

        <RelativeLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:id="@+id/contentInfo"
                android:paddingBottom="48sp"
                android:clipToPadding="false"
                android:orientation="vertical">

                <ProgressBar
                    style="?android:attr/progressBarStyle"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/profile_page_left_right_margin"
                    android:layout_gravity="center"
                    android:id="@+id/progressBar"/>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:padding="10dp"
                    android:background="@color/grey_light">

                    <TextView android:id="@+id/ownerTitle"
                        android:clickable="true"
                        android:focusable="true"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/keyline_1"
                        android:layout_gravity="center_vertical"
                        android:textAppearance="@style/TextAppearance.AppCompat.Body1"
                        android:textColor="@color/body_text_1" />

                </LinearLayout>

                <LinearLayout
                    android:orientation="horizontal"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginTop="@dimen/profile_image_margin"
                    android:padding="10dp">

                    <com.bigsteptech.seandroidnativeapp.Classes.Modules.Common.ViewRelated.ExpandableTextView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginLeft="@dimen/keyline_1"
                        style="@style/TextBody"
                        android:layout_gravity="center_vertical"
                        android:id="@+id/view_description" />

                </LinearLayout>

                <android.support.design.widget.TabLayout
                    android:id="@+id/slidingTabs"
                    android:layout_width="match_parent"
                    app:tabIndicatorHeight="3dp"
                    app:tabMode="scrollable"
                    android:layout_height="wrap_content"/>

                <android.support.v4.view.ViewPager
                    android:id="@+id/pager"
                    android:layout_width="match_parent"
                    android:layout_height="300dp">

                </android.support.v4.view.ViewPager>


            </LinearLayout>


        </RelativeLayout>

    </android.support.v4.widget.NestedScrollView>

    <android.support.design.widget.FloatingActionButton
        android:layout_height="wrap_content"
        android:layout_width="wrap_content"
        app:layout_anchor="@id/appbar"
        android:id="@+id/joinGroupButton"
        app:layout_anchorGravity="bottom|right|end"
        android:src="@drawable/ic_action_new"
        android:layout_margin="@dimen/fab_margin"
        android:clickable="true"/>

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

The fragments which are having listview, gridview do not scroll to up in collapsing toolBar, how can I achieve this working..

Please help me, thank you so much in advanced...

like image 349
Prithniraj Nicyone Avatar asked Aug 10 '15 07:08

Prithniraj Nicyone


People also ask

What is Collapsing Toolbar layout?

Android CollapsingToolbarLayout is a wrapper for Toolbar which implements a collapsing app bar. It is designed to be used as a direct child of a AppBarLayout.

What is contentScrim in Android?

app:contentScrim: Specifies drawable or Color value when scrolled sufficiently off screen. app:layout_collapseMode: Specifies how child views of collapsing toolbar layout move when layout is moving. Parallax- moves in parallax fashion, Pin-view placed in fixed position.


1 Answers

You need put your ViewPager inside AppBar section and setup layout_behavior :

<android.support.design.widget.AppBarLayout>>
    <android.support.design.widget.CollapsingToolbarLayout>
     // THIS VIEWS WILL BE COLLAPSED
    </android.support.design.widget.CollapsingToolbarLayout>

    // THIS VIEWS WILL BE PINNED
    <android.support.v4.view.ViewPager
          android:id="@+id/pager"
          android:layout_width="match_parent"
          android:layout_height="300dp"
          app:layout_behavior="@string/appbar_scrolling_view_behavior">

    </android.support.v4.view.ViewPager>
</android.support.design.widget.AppBarLayout>

If you using Fragments as children of ViewPager you need setup layout_behavior to each fragment inside ViewPager too.

app:layout_behavior="@string/appbar_scrolling_view_behavior"
like image 154
Sergey Shustikov Avatar answered Sep 21 '22 08:09

Sergey Shustikov