简体   繁体   中英

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/

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM