Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Scroll on top of the CoordinatorLayout and AppBarLayout inside it

Tags:

android

scroll

I have layout like this

    <?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/linearMain"
    android:layout_width="match_parent"
    android:layout_height="990dp"
    android:orientation="vertical">

    <android.support.v4.widget.DrawerLayout xmlns:app="http://schemas.android.com/apk/res-auto"
        android:id="@+id/drawer_layout"
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <android.support.v4.widget.SwipeRefreshLayout
            android:id="@+id/contentView"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <android.support.design.widget.CoordinatorLayout
                android:id="@+id/main_content"
                android:layout_width="match_parent"
                android:layout_height="wrap_content">

                <android.support.design.widget.AppBarLayout
                    android:id="@+id/appbar"
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:background="@color/home_background">

                    <include
                        android:id="@+id/toolbar"
                        layout="@layout/toolbar" />

                    <FrameLayout
                        android:id="@+id/frameAdvertiseSlider"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical"
                        app:layout_scrollFlags="scroll|snap">

                        <android.support.v4.view.ViewPager
                            android:id="@+id/viewPagerAdvertiseSlider"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/pager_advertise_slider_height"
                            android:layout_gravity="top" />

                        <com.viewpagerindicator.CirclePageIndicator
                            android:id="@+id/indicatorAdvertiseSlider"
                            style="@style/IntroPageIndicator"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_gravity="bottom"
                            android:layout_marginBottom="@dimen/margin_medium" />

                    </FrameLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutMainCategory"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginTop="@dimen/home_elements_space"
                        android:orientation="horizontal"
                        app:layout_scrollFlags="scroll|snap">

                        <Button
                            android:id="@+id/buttonMainCategory1"
                            style="@style/ButtonHomeStyle"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_weight="1"
                            android:drawableTop="@drawable/ic_category"
                            android:paddingBottom="12dp"
                            android:paddingTop="15dp"
                            android:text="@string/home_category_category" />

                        <Button
                            android:id="@+id/buttonMainCategory2"
                            style="@style/ButtonHomeStyle"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="1dp"
                            android:layout_marginStart="1dp"
                            android:layout_weight="1"
                            android:drawablePadding="5dp"
                            android:drawableTop="@drawable/ic_action"
                            android:paddingTop="15dp"
                            android:text="@string/home_category_action" />

                        <Button
                            android:id="@+id/buttonMainCategory3"
                            style="@style/ButtonHomeStyle"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="1dp"
                            android:layout_marginStart="1dp"
                            android:layout_weight="1"
                            android:drawablePadding="5dp"
                            android:drawableTop="@drawable/ic_new"
                            android:paddingTop="15dp"
                            android:text="@string/home_category_new" />

                        <Button
                            android:id="@+id/buttonMainCategory4"
                            style="@style/ButtonHomeStyle"
                            android:layout_width="match_parent"
                            android:layout_height="match_parent"
                            android:layout_marginLeft="1dp"
                            android:layout_marginStart="1dp"
                            android:layout_weight="1"
                            android:drawablePadding="5dp"
                            android:drawableTop="@drawable/ic_most_selling"
                            android:paddingTop="5dp"
                            android:text="@string/home_category_most_selling" />
                    </LinearLayout>

                    <LinearLayout
                        android:id="@+id/linearLayoutBanners"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/home_elements_space"
                        android:layout_marginTop="@dimen/home_elements_space"
                        android:orientation="horizontal"
                        app:layout_scrollFlags="scroll|snap">

                        <ImageView
                            android:id="@+id/imageBanner1"
                            android:layout_width="0dp"
                            android:layout_height="@dimen/banner1_height"
                            android:layout_weight="1"
                            android:scaleType="fitXY" />

                        <LinearLayout
                            android:layout_width="0dp"
                            android:layout_height="@dimen/banner1_height"
                            android:layout_marginLeft="1dp"
                            android:layout_marginStart="1dp"
                            android:layout_weight="1"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/imageBanner2"
                                android:layout_width="match_parent"
                                android:layout_height="@dimen/banner2_height"
                                android:scaleType="fitXY" />

                            <View
                                android:layout_width="match_parent"
                                android:layout_height="1dp"
                                android:background="#00000000" />

                            <ImageView
                                android:id="@+id/imageBanner3"
                                android:layout_width="match_parent"
                                android:layout_height="@dimen/banner2_height"
                                android:scaleType="fitXY" />
                        </LinearLayout>
                    </LinearLayout>

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginBottom="@dimen/home_elements_space"
                        android:orientation="vertical"
                        app:layout_scrollFlags="scroll|snap">

                        <android.support.design.widget.TabLayout
                            android:id="@+id/tabLayoutNews"
                            style="@style/MyCustomTabLayout"
                            android:layout_width="match_parent"
                            android:layout_height="@dimen/tabs_height"
                            android:background="@color/white"
                            app:tabIndicatorColor="@color/home_tab_indicator"
                            app:tabMode="fixed"
                            app:tabSelectedTextColor="@color/home_tab_color"
                            app:tabTextColor="@color/home_tab_color" />

                        <android.support.v4.view.ViewPager
                            android:id="@+id/viewPagerNews"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:background="@android:color/white" />

                    </LinearLayout>

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


                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:orientation="vertical"
                    app:layout_behavior="@string/appbar_scrolling_view_behavior">

                    <android.support.design.widget.TabLayout
                        android:id="@+id/tabLayoutPager"
                        style="@style/MyCustomTabLayout"
                        android:layout_width="match_parent"
                        android:layout_height="@dimen/tabs_height"
                        android:background="@color/white"
                        app:tabGravity="center"
                        app:tabIndicatorColor="@color/home_tab_indicator"
                        app:tabMode="scrollable"
                        app:tabSelectedTextColor="@color/home_tab_color"
                        app:tabTextColor="@color/home_tab_color" />

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

                </LinearLayout>

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

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

        <android.support.design.widget.NavigationView
            android:id="@+id/navigation_view"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_gravity="start"
            android:fitsSystemWindows="true">

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:orientation="vertical">

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

                    <ImageView
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:scaleType="centerCrop"
                        android:src="@drawable/home_profile_background" />

                    <LinearLayout
                        android:id="@+id/linearProfile"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_margin="@dimen/margin_medium"
                        android:orientation="vertical">

                        <rs.netlink.android.widgets.CircleImageView
                            android:id="@+id/circleImageViewProfile"
                            android:layout_width="@dimen/profile_image_size"
                            android:layout_height="@dimen/profile_image_size"
                            android:scaleType="centerCrop"
                            android:src="@drawable/default_profile_image"
                            app:border_color="@color/home_profile_image_ellipse"
                            app:border_width="1dp" />

                        <TextView
                            android:id="@+id/textName"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/margin_small_medium"
                            android:textColor="@color/home_profile_image_ellipse"
                            android:textSize="@dimen/text_small_normal" />

                        <TextView
                            android:id="@+id/textEmail"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:layout_marginTop="@dimen/margin_mini_small"
                            android:textColor="@color/home_profile_image_ellipse"
                            android:textSize="@dimen/text_micro_mini" />

                    </LinearLayout>

                </RelativeLayout>

                <ScrollView
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    android:layout_marginTop="@dimen/margin_small">

                    <LinearLayout
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:gravity="center_horizontal"
                        android:orientation="vertical">

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuHome"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_menu_home"
                            android:drawableStart="@drawable/ic_menu_home"
                            android:text="@string/menu_home"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuProfile"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_menu_profile"
                            android:drawableStart="@drawable/ic_menu_profile"
                            android:text="@string/menu_profile"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuBasket"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_menu_basket"
                            android:drawableStart="@drawable/ic_menu_basket"
                            android:text="@string/menu_basket"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuWishList"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_menu_wish_list"
                            android:drawableStart="@drawable/ic_menu_wish_list"
                            android:text="@string/menu_wish_list"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuWhereToBay"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_where_to_bay"
                            android:drawableStart="@drawable/ic_where_to_bay"
                            android:text="@string/menu_where_to_buy"
                            app:font="@string/font_bold" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="2dp"
                            android:background="@color/home_menu_separator_line" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuSettings"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_menu_settings"
                            android:drawableStart="@drawable/ic_menu_settings"
                            android:text="@string/menu_settings"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuCallCenter"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_call_center"
                            android:drawableStart="@drawable/ic_call_center"
                            android:text="@string/menu_call_centar"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuAboutUs"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_about_us"
                            android:drawableStart="@drawable/ic_about_us"
                            android:text="@string/menu_about_us"
                            app:font="@string/font_bold" />

                        <rs.netlink.android.widgets.MyButton
                            android:id="@+id/buttonMenuTerms"
                            style="@style/ButtonHomeMenuBorderlessWhite"
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:drawableLeft="@drawable/ic_terms"
                            android:drawableStart="@drawable/ic_terms"
                            android:text="@string/menu_terms"
                            app:font="@string/font_bold" />

                    </LinearLayout>
                </ScrollView>
            </LinearLayout>
        </android.support.design.widget.NavigationView>

    </android.support.v4.widget.DrawerLayout>
</LinearLayout>

and I am trying to scroll on top of the CoordinatorLayout or AppBarLayout(they are both on the top of the screen).

When scrolled on the bottom of the screen (on elements in the last ViewPager, there I have RecyclerView) and click on button "go to top" where I call coordinatorLayout.scrollTo(0,0) it does not work.

So where did I make a mistake, or is there some other way to do this.

Thanks.

like image 769
Predrag Kecman Avatar asked Mar 22 '16 15:03

Predrag Kecman


1 Answers

Try this:

appBarLayout.setExpanded(true, /*animate=*/true);
like image 78
Westy92 Avatar answered Oct 19 '22 22:10

Westy92