Skip to content

Instantly share code, notes, and snippets.

@AfzalivE
Last active March 13, 2017 03:16
Show Gist options
  • Save AfzalivE/2d8046579bb195e404ce8a9f6bfa4b1f to your computer and use it in GitHub Desktop.
Save AfzalivE/2d8046579bb195e404ce8a9f6bfa4b1f to your computer and use it in GitHub Desktop.
DisableSwipeBottomSheetBehavior
public class BottomSheetView extends NestedScrollView {
private BottomSheetBehavior bottomSheetBehavior;
private RecyclerView devicesListV;
public BottomSheetView(Context context, AttributeSet attrs) {
super(context, attrs);
LayoutInflater.from(context).inflate(R.layout.bottom_sheet_view, this, true);
setNestedScrollingEnabled(false);
LinearLayoutManager layoutManager = new LinearLayoutManager(context);
devicesListV = (RecyclerView) ((LinearLayout) getChildAt(0)).getChildAt(1);
devicesListV.setLayoutManager(layoutManager);
}
@Override
public void onAttachedToWindow() {
super.onAttachedToWindow();
bottomSheetBehavior = BottomSheetBehavior.from(this);
}
public void setDeviceAdapter(DeviceAdapter deviceAdapter) {
devicesListV.setAdapter(deviceAdapter);
}
public void hide(@ColorRes int oldColor, @ColorRes int newColor) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
int oldColorCompat = ContextCompat.getColor(getContext(), oldColor);
int newColorCompat = ContextCompat.getColor(getContext(), newColor);
ObjectAnimator animator = ObjectAnimator.ofObject(this, "backgroundColor", new ArgbEvaluator(), oldColorCompat, newColorCompat);
animator.setDuration(250);
animator.start();
}
public void show(@ColorRes int oldColor, @ColorRes int newColor) {
bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
int oldColorCompat = ContextCompat.getColor(getContext(), oldColor);
int newColorCompat = ContextCompat.getColor(getContext(), newColor);
ObjectAnimator animator = ObjectAnimator.ofObject(this, "backgroundColor", new ArgbEvaluator(), oldColorCompat, newColorCompat);
animator.setDuration(500);
animator.start();
}
}
public class DisableSwipeBottomSheetBehavior<V extends View> extends BottomSheetBehavior<V> {
private boolean mAllowUserDragging = false;
/**
* Default constructor for instantiating BottomSheetBehaviors.
*/
public DisableSwipeBottomSheetBehavior() {
super();
}
/**
* Default constructor for inflating BottomSheetBehaviors from layout.
*
* @param context The {@link Context}.
* @param attrs The {@link AttributeSet}.
*/
public DisableSwipeBottomSheetBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setAllowUserDragging(boolean allowUserDragging) {
mAllowUserDragging = allowUserDragging;
}
@Override
public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
if (!mAllowUserDragging) {
return false;
}
return super.onInterceptTouchEvent(parent, child, event);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment