Advertisement
Guest User

Untitled

a guest
Jul 23rd, 2019
85
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.86 KB | None | 0 0
  1. LinearLayout bottomSheetViewgroup = (LinearLayout) findViewById(R.id.bottomSheet);
  2.  
  3. bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetViewgroup);
  4.  
  5. bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); //this line
  6.  
  7. package uk.ac.qub.quibe.misc;
  8.  
  9. import android.content.Context;
  10. import android.support.design.widget.CoordinatorLayout;
  11. import android.util.AttributeSet;
  12. import android.view.View;
  13.  
  14. /**
  15. * Created by mcp on 15/03/16.
  16. */
  17. public class ExpandedBottomSheetBehavior<V extends View> extends android.support.design.widget.BottomSheetBehavior<V> {
  18.  
  19. public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
  20. super(context, attrs);
  21. }
  22.  
  23. @Override
  24. public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
  25. SavedState dummySavedState = new SavedState(super.onSaveInstanceState(parent, child), STATE_EXPANDED);
  26. super.onRestoreInstanceState(parent, child, dummySavedState);
  27. return super.onLayoutChild(parent, child, layoutDirection);
  28. /*
  29. Unfortunately its not good enough to just call setState(STATE_EXPANDED); after super.onLayoutChild
  30. The reason is that an animation plays after calling setState. This can cause some graphical issues with other layouts
  31. Instead we need to use setInternalState, however this is a private method.
  32. The trick is to utilise onRestoreInstance to call setInternalState immediately and indirectly
  33. */
  34. }
  35.  
  36. }
  37.  
  38. app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
  39.  
  40. app:layout_behavior="uk.ac.qub.quibe.misc.ExpandedBottomSheetBehavior"
  41.  
  42. public class ExpandedBottomSheetBehavior<V extends View> extends
  43. android.support.design.widget.BottomSheetBehavior<V> {
  44.  
  45. public ExpandedBottomSheetBehavior(Context context, AttributeSet attrs) {
  46. super(context, attrs);
  47. }
  48.  
  49. @Override
  50. public boolean onLayoutChild(final CoordinatorLayout parent, final V child, final int layoutDirection) {
  51. return super.onLayoutChild(parent, child, layoutDirection);
  52. }
  53.  
  54. @Override
  55. public boolean onInterceptTouchEvent(CoordinatorLayout parent, V child, MotionEvent event) {
  56. try {
  57. return super.onInterceptTouchEvent(parent, child, event);
  58. } catch (NullPointerException ignored) {
  59. return false;
  60. }
  61. }
  62. }
  63.  
  64. final View bottomSheet = findViewById(R.id.bottom_sheet);
  65. bottomSheet.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
  66. @Override
  67. public void onGlobalLayout() {
  68. bottomSheet.getViewTreeObserver().removeOnGlobalLayoutListener(this);
  69. bottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
  70. bottomSheetBehavior.setPeekHeight(300);
  71. bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
  72. }
  73. });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement