Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to capture the scroll event in CoordinateLayout in Android

I have two toolbars( one set as "enterAlways" and the other is "scroll") and a recyclerview in a coordinatelayout, what I wanted to achieve is to capture scroll event when the recyclerview has not been scrolled, which means when the toolbar which has been set as "enterAlways" is still scrolling.

But when I tried coordinateLayout.setOnScrollChangeListener() , its not working at all.

my layout is like this:

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

    <EndlessRecyclerView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
       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="#00ff0000"
        >
        <include      app:layout_behavior="com.viki.android.adapter.AbsListener"
            android:id="@+id/header_upper" layout="@layout/inlcude_channelheader_upper" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="scroll"/>
        <include android:id="@+id/header_lower" layout="@layout/include_channelheader_lower" android:layout_width="match_parent" android:layout_height="wrap_content" app:layout_scrollFlags="enterAlways"/>

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

How exactly can I capture the scroll event and also the value for dx and dy like what onScrollListener for listview or recyclerview does.

like image 368
Qing Avatar asked Nov 24 '15 10:11

Qing


People also ask

What is the use of CoordinatorLayout in Android?

CoordinatorLayout is a super-powered FrameLayout . CoordinatorLayout is intended for two primary use cases: As a top-level application decor or chrome layout. As a container for a specific interaction with one or more child views.

How do I disable scrolling in collapsing toolbar layout Android?

The solution is simple, we just need to set the app:scrimAnimationDuration=”0" in our collapsing toolbar layout like the below code snippet. Now just run the code and see the results, you will see then there will be no fading animation anymore.

What is AppBarLayout Android?

AppBarLayout is a vertical LinearLayout which implements many of the features of material designs app bar concept, namely scrolling gestures. Children should provide their desired scrolling behavior through AppBarLayout.


1 Answers

AppBarLayout provides a listener you can implement to listen for when the AppBarLayout's scrolling siblings move up/down. What you want to do is implement that listener and register it with your AppBarLayout as follows:

 mAppBar.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
        @Override
        public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {

            Log.d("tag_scroll", "recycler_view current offset: "+verticalOffset);
        }
    });
like image 79
Ari Avatar answered Sep 17 '22 19:09

Ari