Created
March 18, 2017 15:00
-
-
Save alapshin/fc6faea288da0bde97e7c087e1724e82 to your computer and use it in GitHub Desktop.
BottomSheetBehavior with support for multiple callbacks
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import android.content.Context; | |
import android.support.annotation.NonNull; | |
import android.support.design.widget.BottomSheetBehavior; | |
import android.util.AttributeSet; | |
import android.view.View; | |
import java.util.ArrayList; | |
/** | |
* Wrapper around {@link BottomSheetBehavior} with support for multiple callbacks. | |
*/ | |
public class ProxyBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> { | |
private final BottomSheetCallback proxyCallback = new BottomSheetCallback() { | |
@Override | |
public void onSlide(@NonNull View bottomSheet, float slideOffset) { | |
for (int i = 0; i < callbacks.size(); ++i) { | |
callbacks.get(i).onSlide(bottomSheet, slideOffset); | |
} | |
} | |
@Override | |
public void onStateChanged(@NonNull View bottomSheet, int newState) { | |
for (int i = 0; i < callbacks.size(); ++i) { | |
callbacks.get(i).onStateChanged(bottomSheet, newState); | |
} | |
} | |
}; | |
private final ArrayList<BottomSheetCallback> callbacks = new ArrayList<>(); | |
public ProxyBottomSheetBehavior() { | |
} | |
public ProxyBottomSheetBehavior(Context context, AttributeSet attrs) { | |
super(context, attrs); | |
} | |
public void addBottomSheetCallback(BottomSheetCallback callback) { | |
if (!callbacks.contains(callback)) { | |
callbacks.add(callback); | |
} | |
setBottomSheetCallback(proxyCallback); | |
} | |
public void removeBottomSheetCallback(BottomSheetCallback callback) { | |
callbacks.remove(callback); | |
} | |
public static <V extends View> ProxyBottomSheetBehavior<V> from(V view) { | |
return (ProxyBottomSheetBehavior<V>) BottomSheetBehavior.from(view); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment