Coordinator Behavior on NestedScroll

Viewed 319

I am trying to achieve elastic overscroll on nestedscrollview to actually do something like zooming of appbarlayout when list is overscroll but also when user lifts up his finger the scrollview should bounce back.

My behavior on nestedscrollview is below

 public class Behaviour extends 
    CoordinatorLayout.Behavior<NestedScrollView> {
        private int totaloffset;

        public Behaviour(Context context, AttributeSet attrs) {

        }

        @Override
        public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull NestedScrollView child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) {
            return true;
        }

        @Override
        public void onNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull NestedScrollView child, @NonNull View target, int dxConsumed, int dyConsumed, int dxUnconsumed, int dyUnconsumed, int type) {
            if (dyUnconsumed < 0) {
                child.offsetTopAndBottom(-dyUnconsumed);
                totaloffset = totaloffset + dyUnconsumed;
            }
        }

        @Override
        public void onStopNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull NestedScrollView child, @NonNull View target, int type) {
            super.onStopNestedScroll(coordinatorLayout, child, target, type);
        }

and my activity xml is :

<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.bye.hey.coordinator.MainActivity">

    <android.support.design.widget.AppBarLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:theme="@style/AppTheme.AppBarOverlay">

        <android.support.v7.widget.Toolbar
            android:id="@+id/toolbar"
            android:layout_width="match_parent"
            android:layout_height="?attr/actionBarSize"
            android:background="?attr/colorPrimary"
            app:popupTheme="@style/AppTheme.PopupOverlay" />

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

    <android.support.v4.widget.NestedScrollView
        android:layout_width="match_parent"
        app:layout_behavior=".Behaviour"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <View
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/colorPrimaryDark" />

            <View
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/colorPrimaryDark" />

            <View
                android:layout_width="match_parent"
                android:layout_height="300dp"
                android:background="@color/colorPrimaryDark" />
        </LinearLayout>
    </android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>  

The problem is i dont know how to rebound it back on user finger lift up. I tried ontouchevent but that either stops the normal scroll behavior or give some other weird result. I can easily rebound it back by simply animating the totaloffset. But i dont know where to write that code.

I also tried to implement that in onStopscroll but that is called when list is not scrolling even if user hasnt lifted up his finger

0 Answers
Related