├── MaterialButtonBehavior.java ├── README.md └── preview.gif /MaterialButtonBehavior.java: -------------------------------------------------------------------------------- 1 | package ; 2 | 3 | import android.animation.ValueAnimator; 4 | import android.content.Context; 5 | import android.support.annotation.NonNull; 6 | import android.support.design.button.MaterialButton; 7 | import android.support.design.widget.CoordinatorLayout; 8 | import android.support.v7.widget.RecyclerView; 9 | import android.util.AttributeSet; 10 | import android.view.View; 11 | import android.view.animation.DecelerateInterpolator; 12 | 13 | /** 14 | * Created by ImanX. 15 | */ 16 | public class MaterialButtonBehavior extends CoordinatorLayout.Behavior { 17 | private ValueAnimator valueAnimator = new ValueAnimator(); 18 | private int expandWidth; 19 | private int collapseWidth; 20 | 21 | public MaterialButtonBehavior() { 22 | } 23 | 24 | public MaterialButtonBehavior(Context context, AttributeSet attrs) { 25 | super(context, attrs); 26 | } 27 | 28 | @Override 29 | public boolean onStartNestedScroll(@NonNull CoordinatorLayout coordinatorLayout, @NonNull MaterialButton child, @NonNull View directTargetChild, @NonNull View target, int axes, int type) { 30 | return true; 31 | } 32 | 33 | @Override 34 | public boolean layoutDependsOn(CoordinatorLayout parent, MaterialButton child, View dependency) { 35 | 36 | if (expandWidth == 0 && child.getMeasuredWidth() != 0) { 37 | expandWidth = child.getMeasuredWidth(); 38 | } 39 | 40 | if (collapseWidth == 0 && child.getMinWidth() != 0) { 41 | collapseWidth = child.getMinWidth(); 42 | } 43 | 44 | return dependency instanceof RecyclerView; 45 | 46 | } 47 | 48 | 49 | @Override 50 | public void onNestedScroll(CoordinatorLayout coordinatorLayout, 51 | MaterialButton child, View target, int dxConsumed, 52 | int dyConsumed, int dxUnconsumed, int dy) { 53 | if (dyConsumed < 0) { 54 | //Scrolling down 55 | measure(child, 170, expandWidth); 56 | 57 | } else if (dyConsumed > 0) { 58 | // Scrolling up 59 | measure(child, 170, collapseWidth); 60 | 61 | 62 | } 63 | } 64 | 65 | private void measure(final View v, int duration, int width) { 66 | 67 | if (valueAnimator.isRunning()) { 68 | return; 69 | } 70 | 71 | 72 | int preWidth = v.getMeasuredWidth(); 73 | valueAnimator.setIntValues(preWidth, width); 74 | valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { 75 | @Override 76 | public void onAnimationUpdate(ValueAnimator animation) { 77 | v.getLayoutParams().width = (int) animation.getAnimatedValue(); 78 | v.requestLayout(); 79 | } 80 | }); 81 | valueAnimator.setInterpolator(new DecelerateInterpolator()); 82 | valueAnimator.setDuration(duration); 83 | valueAnimator.start(); 84 | } 85 | 86 | } 87 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Android Material Button Behavior on RecyclerView 2 | 3 | ### Preview 4 | sample 5 | 6 | ### Getting Started 7 | Download `MaterialButtonBehavior` and copy or put your project. 8 | add `MaterialButton` component in your `xml` layout that parent it should be `CoordinatorLayout` 9 | 10 | `android:layout_width` size of expande 11 | 12 | `android:minWidth` size of collapse 13 | 14 | `app:layout_behavior` for set `MaterialButtonBehavior` 15 | 16 | ### Example 17 | ```xml 18 | 21 | . 22 | . 23 | . 24 | 25 | 43 | ``` 44 | -------------------------------------------------------------------------------- /preview.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/ImanX/android-materialButton-behavior/cf344198f1f95bec940e9178dc59d49366c15f3b/preview.gif --------------------------------------------------------------------------------