Skip to content

Instantly share code, notes, and snippets.

@afshin-hoseini
Created October 9, 2016 20:12
Show Gist options
  • Save afshin-hoseini/d1fa7ef7d7801821850c5fab987ef115 to your computer and use it in GitHub Desktop.
Save afshin-hoseini/d1fa7ef7d7801821850c5fab987ef115 to your computer and use it in GitHub Desktop.
Fling behavior for appbar and recyclerview
package ir.abrishamapp.abrishamandroidapp.Activities.OrdersLayer.DetailsViewer;
import android.content.Context;
import android.support.design.widget.AppBarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.widget.RecyclerView;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
/**
* Created by afshinhoseini on 9/14/16.
*/
public final class FlingBehavior extends AppBarLayout.Behavior {
private static final int TOP_CHILD_FLING_THRESHOLD = 0;
private boolean isPositive;
private int verticalOffset = 0;
public FlingBehavior() {
}
public FlingBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean layoutDependsOn(CoordinatorLayout parent, AppBarLayout child, View dependency) {
child.addOnOffsetChangedListener(new AppBarLayout.OnOffsetChangedListener() {
@Override
public void onOffsetChanged(AppBarLayout appBarLayout, int verticalOffset) {
FlingBehavior.this.verticalOffset = verticalOffset;
Log.e("Vertical offset", verticalOffset + "");
}
});
return super.layoutDependsOn(parent, child, dependency);
}
@Override
public boolean onNestedFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY, boolean consumed) {
if (velocityY > 0 && !isPositive || velocityY < 0 && isPositive) {
velocityY = velocityY * -1;
}
if (target instanceof RecyclerView && velocityY != 0) {
final RecyclerView recyclerView = (RecyclerView) target;
final View firstChild = recyclerView.getChildAt(0);
final int childAdapterPosition = recyclerView.getChildAdapterPosition(firstChild);
consumed = childAdapterPosition > TOP_CHILD_FLING_THRESHOLD;
}
Log.e("onNestedFling","Consumed");
return super.onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, consumed);
}
@Override
public boolean onNestedPreFling(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, float velocityX, float velocityY) {
super.onNestedPreFling(coordinatorLayout, child, target, velocityX, velocityY);
if(target instanceof RecyclerView) {
if(isPositive && verticalOffset > -child.getTotalScrollRange()) { // UP
onNestedFling(coordinatorLayout, child, target, velocityX, velocityY, true);
return true;
}
else if(!isPositive) { // DOWN
}
}
return false;
}
@Override
public void onNestedPreScroll(CoordinatorLayout coordinatorLayout, AppBarLayout child, View target, int dx, int dy, int[] consumed) {
super.onNestedPreScroll(coordinatorLayout, child, target, dx, dy, consumed);
isPositive = dy > 0;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment