Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

CoordinatorLayout with RecyclerView

I have a LinearLayout that I want to hide when I scroll up on my RecyclerView, and reappear when I scroll down; the behaviour should be just like how the Toolbar hides and reappears.

This is what I have so far:

<android.support.design.widget.CoordinatorLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

   <LinearLayout
        android:id="@+id/viewToHideOnScroll
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <!-- other stuff inside the LinearLayout -->

   </LinearLayout>

   <RecyclerView
        android:id="@+id/recyclerView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"/>

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

From what I can understand so far, I can specify a app:layout_behavior value on viewToHideOnScroll so that it smooth scrolls in and out of view according to scroll events on recyclerView. To do this, I have to write a custom class ViewToHideOnScrollBehavior and override layoutDependsOn and some other method (onNestedScroll?).

If that is correct, here is what I have:

public class ViewToHideOnScrollBehavior extends CoordinatorLayout.Behavior<LinearLayout> {

public ViewToHideOnScrollBehavior(Context context, AttributeSet attrs) {}

    @Override
    public boolean layoutDependsOn(CoordinatorLayout parent, LinearLayout child, View dependency) {
        return dependency instanceof RecyclerView;
    }

    // some other method to override, I don't know
}

Can someone give me a hint, or am I doing this all wrong?

I've been following https://lab.getbase.com/introduction-to-coordinator-layout-on-android/

like image 399
squeeish Avatar asked Aug 12 '15 04:08

squeeish


1 Answers

you have to put LinearLayout inside AppBar layout when user scroll your linear layout is hide you have to create xml file like below.

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

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

     <LinearLayout
                android:id="@+id/lytSearchBar"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:background="@color/colorPrimary"
                android:gravity="center_vertical"
                android:orientation="horizontal"
                android:padding="@dimen/fivedp"
                app:layout_scrollFlags="scroll|enterAlways" // layout_scrollFlags for scroll layout
                android:visibility="visible">

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

      <android.support.v7.widget.RecyclerView
            android:id="@+id/rvOrderList"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:layout_below="@id/lytSearchBar"
            android:paddingTop="@dimen/tendp"
            app:layout_behavior="@string/appbar_scrolling_view_behavior" />

in RecyclerView don't forget to add attribute app:layout_behaviour as seen in above xml.

like image 199
Darshan Mistry Avatar answered Oct 12 '22 15:10

Darshan Mistry