VLeonovs
VLeonovs

Reputation: 2251

Scrolling problems inside AppBarLayout

I have CoordinatorLayout:

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
                                                 xmlns:app="http://schemas.android.com/apk/res-auto"
                                                 android:layout_width="match_parent"
                                                 android:layout_height="match_parent"
                                                 android:background="@color/background"
                                                 android:clickable="true"
                                                 android:focusableInTouchMode="true">

    <android.support.v7.widget.RecyclerView
        android:id="@+id/bottom_recycle_view"
        android:layout_width="fill_parent"
        android:layout_height="match_parent"
        android:layout_margin="10dp"
        android:background="@drawable/border_gray_solid_background_nop"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>


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

        <com.app.views.textviews.OpSearchView
            android:id="@+id/search_top"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="@dimen/default_margin"
            android:layout_marginRight="@dimen/default_margin"
            android:layout_marginTop="@dimen/_20sdp"
            app:hint="@string/filter_categories"
            app:layout_scrollFlags="scroll"/>

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical"
            android:paddingBottom="@dimen/_10sdp"
            app:layout_scrollFlags="scroll">


            <TextView
                android:id="@+id/top_heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginBottom="@dimen/default_margin"
                android:layout_marginLeft="@dimen/default_margin"
                android:layout_marginRight="@dimen/default_margin"
                android:layout_marginTop="@dimen/home_screen_sector_spacing"
                android:gravity="left"
                android:textColor="@color/heading_gray"
                android:textSize="@dimen/_14sdp"
                android:textStyle="bold"/>


            <com.app.views.recycleview.HorizontalRecycleView
                android:id="@+id/top_recycle_view"
                android:layout_width="match_parent"
                android:layout_height="@dimen/categories_row_height"
                android:clipToPadding="false"
                android:paddingLeft="@dimen/default_margin"/>


            <TextView
                android:id="@+id/bottom_heading"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_marginLeft="@dimen/default_margin"
                android:layout_marginRight="@dimen/default_margin"
                android:layout_marginTop="@dimen/home_screen_sector_spacing"
                android:gravity="left"
                android:textColor="@color/heading_gray"
                android:textSize="@dimen/_14sdp"
                android:textStyle="bold"/>

        </LinearLayout>


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

I would like to make OpSearchView sticky to the top of the layout.

I try to remove app:layout_scrollFlags="scroll" from OpSearchView but all childs from AppBarLayout became sticky.

If I move OpSearchView from the AppBarLayout under RecyclerView, the RecyclerView(with layout_behavior) became invisible.

How to make OpSearchView without scrolling behavior?

http://prntscr.com/ad1jql

Upvotes: 0

Views: 606

Answers (1)

Billy
Billy

Reputation: 1374

<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:clickable="true"
    android:focusableInTouchMode="true"
    >

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="#eeeeee"
        >

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

            <LinearLayout
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical"
                android:layout_marginTop="?android:actionBarSize"
                android:paddingBottom="10dp"
                app:layout_scrollFlags="scroll"
                >

                <TextView
                    android:id="@+id/top_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginBottom="@dimen/single_margin"
                    android:layout_marginLeft="@dimen/single_margin"
                    android:layout_marginRight="@dimen/single_margin"
                    android:gravity="start"
                    android:textStyle="bold"
                    android:text="top heading"
                    />

                <!-- com.app.views.recycleview.HorizontalRecycleView -->
                <View
                    android:layout_width="match_parent"
                    android:layout_height="40dp"
                    android:background="#cccccc"
                    />

                <TextView
                    android:id="@+id/bottom_heading"
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="@dimen/single_margin"
                    android:layout_marginRight="@dimen/single_margin"
                    android:gravity="start"
                    android:textStyle="bold"
                    android:text="bottom heading"
                    />

            </LinearLayout>

            <!-- toolbar with the searchview in it -->
            <android.support.v7.widget.Toolbar
                android:id="@+id/toolbar"
                android:layout_width="match_parent"
                android:layout_height="?android:actionBarSize"
                android:background="#dddddd"
                app:layout_scrollFlags="scroll|enterAlways"
                app:layout_collapseMode="pin"
                >

                <SearchView
                    android:id="@+id/search_top"
                    android:layout_width="match_parent"
                    android:layout_height="match_parent"
                    app:queryHint="Search"
                    app:layout_scrollFlags="scroll"
                    app:layout_collapseMode="pin"
                    />

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

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

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

    <!-- scrolling contents -->
    <android.support.v7.widget.RecyclerView
        android:id="@+id/recycle_view"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        app:layout_behavior="@string/appbar_scrolling_view_behavior"/>

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

This is the layout that I came up with working with your layout, I took a few liberties to make things work, you should make your own changes to get it back to your original layout, like your custom SearchView and HorizontalRecyclerView.

P.S. For layout_width and layout_height, match_parent and fill_parent are the same thing, and you should only use match_parent as per Google's documentation suggests.

Upvotes: 1

Related Questions