Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@alapshin
Created March 18, 2017 15:00
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alapshin/fc6faea288da0bde97e7c087e1724e82 to your computer and use it in GitHub Desktop.
Save alapshin/fc6faea288da0bde97e7c087e1724e82 to your computer and use it in GitHub Desktop.
BottomSheetBehavior with support for multiple callbacks
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