Layout Coordinator with RecyclerView

I have a LinearLayout that I want to hide when I view my RecyclerView and reappear when scrolling down; the behavior should be similar to 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 the value of app:layout_behavior on viewToHideOnScroll so that it scrolls scrolls smoothly and does not display according to scroll events on recyclerView . To do this, I need to write my own ViewToHideOnScrollBehavior class and override layoutDependsOn and another method ( onNestedScroll ?).

If this 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 it all wrong?

I followed https://lab.getbase.com/introduction-to-coordinator-layout-on-android/

+6
source share
1 answer

you need to put LinearLayout in the AppBar layout , when the user scrolls the linear layout, hide that you need to create the xml file as shown 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 do not forget to add the app: layout_behaviour attribute , as shown above in xml.

+18
source

All Articles